-2

I`ve tried many ways to convent a string to a int. This includes atoi() and stringstream
But i still cannot convert it. I have a only numeric string and i want to convert it to a int.

The reason for that is that im receiving data form user using getch() and storing it into a string buffer, but in a certain point i want to convert this buffer to a int for use. So my string would be like that:

string hello = "673";

And i want to transform this value into an interger. EDIT This is not a duplicated questions because i tried all this methods many times and still now work.All of this just crash my program or error an invalid conversion.

4

3 回答 3

0

atoi() should work. Check if your string has non numeric characters in the front or somewhere in it. Could you show us the exact string you are trying to convert into int?

于 2013-08-04T07:31:10.067 回答
0

If it is a c++ style string try stoi() instead of atoi(). It plays nicer with the C++ string class.

于 2013-08-04T07:35:13.173 回答
0

Consider this :-

#include<iostream>
#include<sstream>

int main(void) {
int value;
std::string str("12345");

std::istringstream ss(str);
ss >> value;
std::cout<<value<<std::endl;

}
于 2013-08-04T07:40:17.577 回答