istringstream line("Hello ");
string s;
while(line.good())
{
line >> s;
cout << "s is " << s << endl;
}
Why does it print two times the word Hello? The >> operator should stop at the frist space but then at next iteration it's like it starts again from the beginning. Can you help me please?
#include <iomanip>
#include <iostream>
#include <sstream>
usingnamespace std;
int main()
{
string s;
istringstream ss( "one two three four" );
cout << "message-------- good()- bad()-- eof()-- fail()-\n"
<< left;
do
{
s = "(failed)";
ss >> s;
cout << " "
<< setw( 16 ) << s
<< setw( 8 ) << ss.good()
<< setw( 8 ) << ss.bad()
<< setw( 8 ) << ss.eof()
<< setw( 8 ) << ss.fail()
<< endl;
}
while (ss);
return 0;
}
Try changing things here and there to see how it affects the flags. For example, you can try commenting-out line 17, or putting spaces at the end of the string on line 10.