Coverting string to number

Please refer the the post:
http://www.cplusplus.com/forum/articles/6046/

On line 25 it says myStream >> myNumber where myNumber is initialized to 0 in line 17. I know that it evaluates false if the input is not a number, but I have no idea what the code really says.
Thank you.
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
      stringstream ss ;
      int bob ;

      ss << "5";
      ss >> bob;

      cout << bob;

      return 0;
}

That code wouldn't compile. here's a live version of the stringstream accepting a string literal (number) and then sending it to an integer. Then cout prints it out to the stream.
Last edited on
Use This Handy Function:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int (get_INT(string str))
{
int intValue;
intValue = atoi(str.c_str());
return(intValue);
}
Use This Handy Function:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int (get_INT(string str))
{
int intValue;
intValue = atoi(str.c_str());
return(intValue);
}
it says to read an integer from the stream and put it into myNumber.

The expression also tests to ensure that a number was successfully read.

This is made possible by two things:

first, operator>> returns a reference to the stream object;
second, the stream object is boolean comparable, and it is defined to return true if no error has occurred on the stream since the last time the error flag was cleared.

Topic archived. No new replies allowed.