[LC++]IO in C++

Mark Phillips mark at austrics.com.au
Tue Nov 13 12:05:04 UTC 2001


> > int myInt;
> > string myString="259";
> > istringstream iss(myString);
> > iss>>myInt;

What if we want to do error checking on myString?

What we can do is, straight after the above, do:

if (!iss)
  cout<<"Error!!!\n";

This will pick up a problem with myString for cases
like myString="harry" or myString="h259", but it won't
pick up a problem like myString="25hh9".  With the 
last example it will happily make myInt=25.

Is there an easy way of using iss to tell me whether 
the entire string was a valid integer?  So far the 
only method I can think of is rather messy.

Thanks,

Mark.



More information about the tuxCPProgramming mailing list