0

I am trying to convert a string into int using stoi() but i am getting error that error: ‘stoi’ was not declared in this scope. Here is the given code.

#include <iostream>
#include <string>

int main()
{
    std::string str1 = "45";
    std::string str2 = "3.14159";
    std::string str3 = "31337 with words";
    std::string str4 = "words and 2";

    int myint1 = std::stoi(str1);
    int myint2 = std::stoi(str2);
    int myint3 = std::stoi(str3);
    // error: 'std::invalid_argument'
    // int myint4 = std::stoi(str4);

    std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
    std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
    std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
    //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
}
Shravan40
  • 8,922
  • 6
  • 28
  • 48

1 Answers1

4

stoi is from C++11, you should try with atoi

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • 1
    Better [`strtol`](http://man7.org/linux/man-pages/man3/strtol.3.html) than `atoi` if he has the choice, though that's C99-library. – Deduplicator Oct 27 '14 at 16:02