string stream question

J

JM

Hi,


If you change the same string stream to a new string, why is it that
you have to .clear() it before using it? If I don't I get garbage.

Thanks,
Jason

ex.

string a = "12";
string b = "15";
float fa,fb;

istringstream is;

is.str(a);

is>>fa;

is.clear( ) // must do this....

is.str(b)

is>>fb
 
V

Victor Bazarov

JM said:
Hi,


If you change the same string stream to a new string, why is it that
you have to .clear() it before using it? If I don't I get garbage.

Thanks,
Jason

ex.

string a = "12";
string b = "15";
float fa,fb;

istringstream is;

is.str(a);

is>>fa;

is.clear( ) // must do this....

is.str(b)

is>>fb


It probably because changing strings is not coupled with clearing
any errors. The state of the stream is still "end-of-file", which
you have to clear before you can read again.

Victor
 
J

JM

It probably because changing strings is not coupled with clearing
any errors. The state of the stream is still "end-of-file", which
you have to clear before you can read again.

Victor

That makes sense, thanks Victor.

-JM
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top