Reading string as integer

May 25, 2013 at 9:02pm
Hey

I'm trying to read a string as an integer but want to check for failures. Instead of just accepting all of the numbers up to the "g" below, I would like to check for a failure and report it.
1
2
3
4
5
6
7
8
  string s = "12g4";
int y = atoi(s.c_str());
cout << "string " << s << " is integer " << y << endl; 


istringstream iss(s);
if(iss >> y)
cout << "string " << s << " is integer " << y << endl;  


I have tried something along the sort of checking whether the input stream fails like:

if (atoi(s.c_str()))

or

if(iss >> y)


Can anyone help?
Topic archived. No new replies allowed.