30

I'm trying to use stoi to convert a string to an integer, however it says it's not declared. I have the standard library and the <string> included, but it still says [Error] 'stoi' was not declared in this scope

The code is the following:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
string end, init;
cout << "Introduction" << endl;
cout << "Start time (xx:yy)" << endl;
cin >> init;
string hours0 = init.substr(0,2);
int hours = stoi(hours0);
cout << hours << endl;
system("pause");
return 0;

}

Either tell me why it isn't working, or give me a second option to do it, please.

Chris Tang
  • 567
  • 7
  • 18
user3258512
  • 323
  • 1
  • 3
  • 10

13 Answers13

34

std::stoi was introduced in C++11. Make sure your compiler settings are correct and/or your compiler supports C++11.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • 2
    DevC++ is an IDE not a compiler. Double check the version of compiler supplied with your copy of Dev C++ and upgrade if necessary or add the required command line switch to enable it. – Captain Obvlious Feb 28 '14 at 02:14
  • Since Visual Studio C++ Express is paid only, which would be my best option? Is QT Creator good for C++ programmers? – user3258512 Feb 28 '14 at 02:18
  • 3
    @user3258512, Express is free. – chris Feb 28 '14 at 02:18
  • @chris I found Express was too complicated, I could not just write my code and compile/debug as in Dev C++. Any suggestion? – user3258512 Feb 28 '14 at 03:00
  • @user3258512: If you're still fixated on using Dev-C++, there's an updated fork called Orwell that seems to have C++11 support: sourceforge.net/projects/orwelldevcpp/files/Setup%20Releases/ – nneonneo Feb 28 '14 at 06:03
24

The answers above are correct, but not well explained.

g++ -std=c++11 my_cpp_code.cpp

Add -std=c++11 to your compiler options since you are most likely using an older version of debian or ubuntu which is not using by default the new c++11 standard of g++/gcc. I had the same problem on Debian Wheezy.

http://en.cppreference.com/w/cpp/string/basic_string/stol

shows in really small writing to the right in green that c++11 is required.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Eamonn Kenny
  • 1,926
  • 18
  • 20
10

stoi is a C++11 function. If you aren't using a compiler that understands C++11, this simply won't compile.

You can use a stringstream instead to read the input:

stringstream ss(hours0);
ss >> hours;
nneonneo
  • 171,345
  • 36
  • 312
  • 383
10

stoi is available "since C++11". Make sure your compiler is up to date.

You can try atoi(hours0.c_str()) instead.

Daniel
  • 4,481
  • 14
  • 34
7

instead of this line -

int hours = stoi(hours0);

write this -

int hours = atoi(hours0.c_str());

Reference : int atoi(const char *str)

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
pistol_pete
  • 71
  • 1
  • 1
1

In comments under another answer, you indicated you are using a dodgy version of g++ under MS Windows.

In this case, -std=c++11 as suggested by the top answer would still not fix the problem.

Please see the following thread which does discuss your situation: std::stoi doesn't exist in g++ 4.6.1 on MinGW

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
1

I came across this error while working on a programming project in c++,

  1. atoi(),stoi() is not part of the old c++ library in g++ so use the below options while compiling g++ -std=c++11 -o my_app_code my_app_code.cpp
  2. Include the following file in your code #include < cstdlib >

This should take care of the errors

Nnaik
  • 160
  • 1
  • 3
0

Are you running C++ 11? stoi was added in C++ 11, if you're running on an older version use atoi()

Sisnett
  • 101
  • 5
0

Install the latest version of TDM-GCC here is the link-http://wiki.codeblocks.org/index.php/MinGW_installation

0
#include <algorithm>

Include this and then you can compile it using...

g++ -Wall -std=c++11 test.cpp -o test

You can also add "cd /d %~dp0" as the first line of a .bat file in the same directory as your source file so all you have to do is double click on the .bat file for an "automated" compilation.

Hope this helps!

Doug
  • 73
  • 1
  • 8
0

Add this option: -std=c++11 while compiling your code

g++ -std=c++11 my_cpp_code.cpp
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Nassima Noufail
  • 177
  • 1
  • 2
0

I managed to fix this problem by adding the following lines to my CMakeLists.txt:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -Wall  -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall   -O3 -march=native")

# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
   add_definitions(-DCOMPILEDWITHC11)
   message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
   add_definitions(-DCOMPILEDWITHC0X)
   message(STATUS "Using flag -std=c++0x.")
else()
   message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

As mentioned by the other fellows, that is the -std=c++11 issue.

Farid Alijani
  • 839
  • 1
  • 7
  • 25
0

I needed to add std:: when calling the function:

int hours = std::stoi(hours0);
Tal Haham
  • 1,500
  • 12
  • 16