An easy stringstream question

U

utab

Dear all

the code below, there is sth wrong with string streams. I can not
bound the same string to a string stream second time. If I create
another string stream it is OK. Most probably there is sth that I am
missing about stream buffers.

1 #include <iostream>
2 #include <sstream>
3 #include <string>
4
5 using namespace std;
6
7 int main(){
8 unsigned index;
9 unsigned i;
10 char c;
11 double val;
12 string in("1 c 0.43");
13 istringstream strm;
14 strm.str(in);
15 strm >> index >> c >> val;
16 cout << index << " " << c << " " << val*0.2 << endl;
17 //istringstream strm1(in.c_str());
18 strm.str(in);
19 strm >> i;
20 cout << val << " " << i << endl;
21 return 0;
22 }

Output is:
1 c 0.086
0.43 3086289584

Best regards
 
V

Victor Bazarov

utab said:
the code below, there is sth wrong with string streams. I can not
bound the same string to a string stream second time. If I create
another string stream it is OK. Most probably there is sth that I am
missing about stream buffers.

1 #include <iostream>
2 #include <sstream>
3 #include <string>
4
5 using namespace std;
6
7 int main(){
8 unsigned index;
9 unsigned i;
10 char c;
11 double val;
12 string in("1 c 0.43");
13 istringstream strm;
14 strm.str(in);
15 strm >> index >> c >> val;
16 cout << index << " " << c << " " << val*0.2 << endl;
17 //istringstream strm1(in.c_str());

At this point 'strm' is most likely not in a good state. Consider
calling 'clear' for it.
18 strm.str(in);
19 strm >> i;
20 cout << val << " " << i << endl;
21 return 0;
22 }

Output is:
1 c 0.086
0.43 3086289584

Best regards

V
 
U

utab

At this point 'strm' is most likely not in a good state. Consider
calling 'clear' for it.

I have also found that but what is the reason that it is not in good
state.

Thanks
 
V

Victor Bazarov

utab said:
I have also found that but what is the reason that it is not in good
state.

The last read (into a number) ends by trying to read past the end of
the string. That puts it into "end-of-file" state. You can try adding
a space (or other non-digit character) into your sting past the last
number and your stream will not acquire the 'eof' state while reading
the number.

V
 
M

Marcus Kwok

utab said:
the code below, there is sth wrong with string streams. I can not
bound the same string to a string stream second time. If I create
another string stream it is OK. Most probably there is sth that I am
missing about stream buffers.

1 #include <iostream>
2 #include <sstream>
3 #include <string>
4
5 using namespace std;
6
7 int main(){
8 unsigned index;
9 unsigned i;
10 char c;
11 double val;
12 string in("1 c 0.43");
13 istringstream strm;
14 strm.str(in);
15 strm >> index >> c >> val;
16 cout << index << " " << c << " " << val*0.2 << endl;
17 //istringstream strm1(in.c_str());

Try adding:

strm.clear();

here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top