+ 3

stringstream

would anyone explain to me why the following code returns "Helloworld!world!" string example="Hello world!",temp; stringstream mystr(example); while (mystr){ mystr>>temp; cout <<temp; } but this one returns "Helloworld!", assuming that the sstream and string files are included?? string example="Hello world!"; stringstream mystr(example); while (mystr){ string temp; mystr>>temp; cout <<temp; }

20th Sep 2018, 8:09 AM
Moses Odhiambo
Moses Odhiambo - avatar
1 Answer
+ 3
Check the "good" attribute to see if the stream is good for extraction: while(mystr.good()) { // your code here } Or alternatively, check if extraction went well in the loop: while(mystr >> temp) { // your code here } Hth, cmiiw
22nd Sep 2018, 6:35 AM
Ipang