Hi. I would like to know a simple way of converting string to int and int to string and so on. In C# I just had to type Convert.ToInt16(). Super simple. Is there a function or something that can convert different variables to eachother?
I tried this "atoi" I found on tutorials, but it doesn't work;
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <stdlib.h>
#include <iostream>
usingnamespace std;
int main ()
{
int i;
string x;
cin >> x;
i = atoi(x);
cout << endl << i;
}
It gives me this error;
LINE 11: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)
also there is stoi() (stod(), stold()...) function defined in <string> header which basicly atoi() for strings. You can also pass someString.c_str() to atoi() function.