Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ stoi not resolving using eclipse to compile

Tags:

c++

eclipse

This code fails to compile with an error that it can't resolve stio. Have I made some newbie mistake here?

Eclipse Version: 3.8.1 Mint KDE should all be up to date.

GCC Version: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609

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

using namespace std;

int main() {
    string numberGuessed;
    int intNumberGuessed = 0;
    int answer = 0;

    answer = (rand() % 100) + 1;

    do {
        cout << "Guess a number "; // prints !!!Hello World!!!
        getline(cin, numberGuessed);
        intNumberGuessed = stoi(numberGuessed);
        cout << "You guessed "<< numberGuessed << endl;
        cout << "You are not correct. Try again" << endl;
    } while (answer != intNumberGuessed);

cout << "you got it";
return 0;
}

The error message.

16:39:14 **** Incremental Build of configuration Debug for project Hello2 ****
make all 
Building file: ../src/Hello2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Hello2.d" - 
MT"src/Hello2.d" -o "src/Hello2.o" "../src/Hello2.cpp"
../src/Hello2.cpp: In function ‘int main()’:
 ../src/Hello2.cpp:27:40: error: ‘stoi’ was not declared in this scope
  intNumberGuessed = stoi(numberGuessed);
                                    ^
make: *** [src/Hello2.o] Error 1
src/subdir.mk:18: recipe for target 'src/Hello2.o' failed

16:39:14 Build Finished (took 613ms)
like image 883
Douglas E Knapp Avatar asked Aug 05 '26 09:08

Douglas E Knapp


1 Answers

The std::stoi function is available since the c++11 standard.

Apparently your compiler version of GCC is too old, to take c++11 as the current default standard.

You may try to specify the -std=c++11 or -std=c++0x compiler flags, or update your gcc compiler to one of the most recent versions.

Here's a link explaining in detail how to set the compiler flags.

This might help you with updating your compiler version to the latest.

like image 197
πάντα ῥεῖ Avatar answered Aug 07 '26 21:08

πάντα ῥεῖ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!