anyone know how to convert strings into integers?
strings I meant not chars, I meants strings.
#include <string.h>
Printable View
anyone know how to convert strings into integers?
strings I meant not chars, I meants strings.
#include <string.h>
simple enough
PHP Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "1234";
int i = atoi(s.c_str());
cout << i << endl;
return 0;
}
just for future knowledge the opposite is itoa:cool:
Use the atoi() function.
_itoa is a non-standard extension of MS to the CRT. The C standard would be sprintf.
For C++, the standard method would be a stringstream:
Does this make you happy parksie? :DCode:#include <sstream>
#include <string> // no .h, this is old standard and deprecated
#include <iostream>
using namespace std;
int main()
{
// int to string:
ostringstream os;
os << 1249 << " " << 2.34e12;
string s = os.str();
cout << s << endl;
// string to int
istringstream is(s);
int i;
double d;
is >> i >> d;
cout << i << " " << d << endl;
return 0;
}
Yep :D
string.h is deprecated in C++ in favour of <cstring>, which is identical apart from placing everything inside namespace std :)