The program must translate the string into an integer. I tried to use the stoi function for this purpose, but it is not accepted - an error occurs:

 'stoi' was not declared in this scope 

In particular, I need to translate a string like such "5678906576" into an integer. I tried to replace it with atoi , using previously c_str . Writing:

 string str = "5678906576"; cout << atoi(str.c_str())<< endl; 

I 1383939280 in the console 1383939280 .

Then I try the fromStdString().toInt() function from Qt Core . Writing:

 string str = "5678906576"; cout << QString::fromStdString(str).toInt() << endl; 

I get 0.

Gentlemen, please explain why this and with what then should I replace the stoi .

I use Qt 4.7.4 QtCreator-2.4.1.

  • 3
    I will add. You have 5678906576 - this is not int, this is long - Dejsving
  • one
    Yeah, and the function, respectively, toLong () - Evgeny Shmidt
  • 3
    I will not stoi you much if I say that for your example, stoi will not save you? you need a stoll - Harry

2 answers 2

So

 string str = "5678906576"; cout << atoi(str.c_str())<< endl; 

can. But here you have a problem going beyond the int , here you need

 cout << atoll(str.c_str())<< endl; 
  • And what is the maximum number we can shove into int? - Piop
  • @Piop numeric_limits<int>::max() :) On 32-bit platforms - 2 ^ 31-1 = 2147483647 - Mikhailo

To use stoi, you need to include a header file.

 #include <string> 

and use it like this

 #include <string> int main() { std::string str1 = "45"; std::string str2 = "3.14159"; int myint1 = std::stoi(str1); int myint2 = std::stoi(str2); } 

the function itself and usage are described in std :: stoi, std :: stol, std :: stoll