+ 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; }
1 Resposta
+ 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