| | |
working with stringstream
![]() |
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
I am storing some data in a sting & I'm retriving it using stringstream but the problem is that it repeates last element two times.I could not understand why is this happning?Is there anyone who can help me on solving this problem?
Programme code is as follows:::::::::::::
Programme code is as follows:::::::::::::
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<sstream> using namespace std; int main() { int i,x; stringstream sstr; string line; for(i=0;i<3;i++) { getline(cin,line); sstr<<line; while(sstr) { sstr>>x; cout<<x<<" "; //output repeates last element two times } cout<<endl; sstr.clear(); } return 0; }
Last edited by Ancient Dragon; Oct 19th, 2008 at 8:05 am. Reason: add code tags
Reason is that sstr is set to false AFTER trying to read after EOF. So first you read last item, it's ok, when while sees no error, continues, but you can't read more, so x is assigned to last good reading (last number) AND then sstr is set to FALSE!
You need this:
You need this:
while (sstr>>x){//do stuff with x} ![]() |
Similar Threads
- How to get the current working directory (C++)
- Creating Calulator in C (C++)
- I need some help. its an easy problem but... (C++)
- extracting an arbitrary number of numbers from a string (C)
- Parse or Tokenize String (C++)
- Hey Just started new topic with arrays, and having some trouble please help (C++)
- Help Needed on C++ assignment (C++)
- Scanning a text file for a string (C++)
- strstream problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help removing number from an array
- Next Thread: writing functions.
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





