Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function stoi not declared

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.

like image 544
user3258512 Avatar asked Feb 28 '14 02:02

user3258512


People also ask

What is stoi function in C++?

In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings.

In which library is stoi function?

std::stoi is a standard library function, not a keyword. A keyword is something like for or new .

How do I enable C++ 11 in Dev CPP?

To do this, click on Tools in Dev-C++ IDE. Under this click the “Settings” tab. Inside the settings tab, we can see the “Code generation” tab. Click on the “Language Standard (-std)” value and set it to “ISOC++11” or “GNUC++11” as per your requirement.

What is the difference between Atoi and stoi?

Using atoi() First, atoi() converts C strings (null-terminated character arrays) to an integer, while stoi() converts the C++ string to an integer. Second, the atoi() function will silently fail if the string is not convertible to an int , while the stoi() function will simply throw an exception.


Video Answer


1 Answers

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

like image 188
Captain Obvlious Avatar answered Sep 20 '22 11:09

Captain Obvlious