| | |
working with stringstream
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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 arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





