Thomas Hutton
Thomas Hutton

Reputation: 803

Stoi was not declared in scope - Code::blocks

Edit: I'm trying to tell it to work with C++11 by clicking "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.

I'm getting stoi was not declared in scope, and I've added c++11 to Code::Blocks; I've added compatibility in Settings -> Compilers -> Compiler flags, but it still keeps giving me that error.

And when I try to do atoi or strtol I get the following error:

C:\Users\user\Desktop\Programming\NewProject\main.cpp|19|error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'|

My code:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

    string numberGuessed;
    int numberGuessedint = 0;

    do {

        cout << "Guess a number between 1 and 10: ";
        getline(cin, numberGuessed);
        numberGuessedint = stoi(numberGuessed);
        cout << numberGuessedint << endl;


    } while(numberGuessedint != 4);

    cout << "You win!" << endl;

    return 0;

}

Upvotes: 2

Views: 13849

Answers (3)

Shivam Taneja
Shivam Taneja

Reputation: 21

I am writing a solution which worked for me. As I found in most of the solutions posted on stack overflow, code blocks earlier versions contain a bug. So I deleted my older code blocks version and installed a new version 17.12 from code blocks website.

Then I just clicked on "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.

Settings -> Compilers -> Compiler flags.

It works for me(I am using windows 7).

Upvotes: 1

Revolver_Ocelot
Revolver_Ocelot

Reputation: 8785

It is a known bug in MinGW bundled with Code::Blocks.

You can apply a patch: http://tehsausage.com/mingw-to-string

Or download fresh version of MinGW (preferable with threading support, as you lack it too) and replace one you have right now.

Upvotes: 4

To use atoi you need:

        numberGuessedint = atoi(numberGuessed.c_str());

Upvotes: 1

Related Questions