4

So this error have been addressed several times, but no answers helped me. I'm using Notepad++ and Cygwin on windows 10. My code is as follows and it's from Derek Banas's 1 hour C++ tutorial:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
#include <sstream> 
//#include <stdlib.h>
using namespace std;
int main(){
    string numberGuessed;
int intNumberGuessed = 0;
 do {
    cout << "Guess between 1 and 10: ";
     getline (cin,numberGuessed);
     intNumberGuessed = stoi(numberGuessed);
    cout << intNumberGuessed << endl;
 } while (intNumberGuessed != 4);
   cout << "You Win" << endl;
    return 0;
}

and this is the error I get:

$ g++ -std=c++11 -static ctut.cpp
ctut.cpp: In function ‘int main()’:
ctut.cpp:15:43: error: ‘stoi’ was not declared in this scope
      intNumberGuessed = stoi(numberGuessed);

You see I already have applied all the suggestions in previous answered threads. Is there anything I'm missing? Do I have to start using Ming? Since Notepadd++ is the one I found with the most upvotes in another topic here. This is what I found and tried but didn't work: Function stoi not declared

Community
  • 1
  • 1
Mohsen
  • 314
  • 1
  • 4
  • 14
  • what if you replace `stoi` by `std::stoi` ? – oLen Jan 03 '16 at 19:43
  • @oLen There's already `using namespace std;`? – πάντα ῥεῖ Jan 03 '16 at 19:44
  • @oLen ``$ g++ -std=c++11 -static ctut.cpp ctut.cpp: In function ‘int main()’: ctut.cpp:15:25: error: ‘stoi’ is not a member of ‘std’ intNumberGuessed = std::stoi(numberGuessed);`` – Mohsen Jan 03 '16 at 19:45
  • So you're setting the `-std=c++11` option when compiling? – πάντα ῥεῖ Jan 03 '16 at 19:46
  • @πάνταῥεῖ yes `$ g++ -std=c++11 -static ctut.cpp` I have it tried with or without these options. – Mohsen Jan 03 '16 at 19:48
  • @Mohsen Which exact version of GCC are you using? – πάντα ῥεῖ Jan 03 '16 at 19:49
  • @πάνταῥεῖ how can I find the answer to this question? I'm using the latest offered in Cygwin during the installation. – Mohsen Jan 03 '16 at 19:50
  • @Mohsen `g++ --version` – Gary Jackson Jan 03 '16 at 19:53
  • @GaryJackson thanks. 4.9.3 then. – Mohsen Jan 03 '16 at 19:54
  • @Mohsen I think it must be a Cygwin problem. I can't reproduce the error and I'm using 4.9.3 as well. – Gary Jackson Jan 03 '16 at 19:58
  • Try -std=c++0x. Maybe it works then. – itmuckel Jan 03 '16 at 20:00
  • @GaryJackson Notepad++ and Cygwin is the best way to run C++ in Windows isn't it? Or do you suggest another way? I might not use this function at all in my project but this error is bugging me so much I have stopped learning C++ the last 24 hours to find a solution! Or should I just leave it there? Any work arounds? Can you suggest another function to do this? I have already tried atoi but I get the very same error. – Mohsen Jan 03 '16 at 20:04
  • @Micha90 I tried and got the same error. ``$ g++ -std=c++0x -static ctut.cpp ctut.cpp: In function ‘int main()’: ctut.cpp:15:43: error: ‘stoi’ was not declared in this scope intNumberGuessed = stoi(numberGuessed);`` – Mohsen Jan 03 '16 at 20:06
  • @Fireho yes `strtol` works the way it's mentioned there. Thanks for the link. I can't understand the variables 0 and 10 though but this `atoi` mentioned in the answer works just fine. I now figure out why I'm getting this error based on your link. Thanks. – Mohsen Jan 03 '16 at 20:16

1 Answers1

2

I'm not able to comment yet :( but you could use atoi(numberGuessed.c_str()) instead of stoi().

Dimitri Podborski
  • 3,714
  • 3
  • 19
  • 31