0

I'm using Code::Blocks 16.01 on Windows 10.

I need to convert a string to an integer.

So I'm trying to use stoi, but it says it's not declared.

I have enabled -std=c++11 in compiler settings but it still gives me an error:

'stoi' was not declared in this scope

Screenshot of Code::Blocks:

Screenshot of Code::Blocks

The part of the code which causes the error is:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;


int main() {
    int n,m,k;
    string nmk;
    ifstream test("input00.txt");
    if (test.is_open())
    {
        getline (test,nmk,' ');
        n = stoi (nmk,NULL,10);
        getline (test,nmk,' ');
        m = stoi (nmk,NULL,10);
        getline (test,nmk, '\n');
        k = stoi (nmk,NULL,10);
    }
}
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
luffy
  • 105
  • 8
  • This isn't a [mcve]. You haven't shown any #include statements for example. – DavidW Oct 14 '17 at 08:47
  • I'm a first time user of StackOverflow. Corrected the mistake. – luffy Oct 14 '17 at 12:04
  • That's better. Unfortunately it works fine for me (on gcc linux; I don't have codeblocks) once I add a final closing bracket so I don't know the answer, but hopefully someone else will – DavidW Oct 14 '17 at 12:16
  • The code does run on visual studio 2017 but not on code blocks, if that helps!! – luffy Oct 14 '17 at 14:57
  • Don't use `using namespace std` because it pollutes the global namespace. Instead use `std::stoi()`. Switch to Visual Studio. It's free and works. – Mushy Oct 14 '17 at 15:20
  • I tried `std::stoi()`. It says std doesn't have member `stoi()`. – luffy Oct 15 '17 at 17:02
  • I'd guess that codeblocks just has an old enough version of gcc that it doesn't include the full c++11 library. – DavidW Oct 17 '17 at 18:39
  • Possible duplicate of [Stoi was not declared in scope - Code::blocks](https://stackoverflow.com/questions/34243888/stoi-was-not-declared-in-scope-codeblocks) – n. m. could be an AI Jul 10 '18 at 19:00

0 Answers0