1

I'm practicing my C++ by coding simple input/ouput from user. I have now been searching for over an hour on how to convert a string to an int. Every answer is different, and require something else.

I'm using Code::Blocks v.13.12. MingW32 folder is added to environment variables. I ended up using stoi to convert my string to integer with try/catch block. I've added C++11 support in Code::Blocks by going to Settings -> Compiler and tick Have g++ follow the C++11 ISO language standard [-std=c++11]. It compiles and runs fine in CB, but when I'm trying to compile manually in the command line i get this error:

error: 'stoi' was not declared in this scope. The line it is refering to: characters = stoi(input);

Thise are the commands I've tried so far: g++ -std=c++11 main.cpp & g++ -std=c++11 -c main.cpp -o main.o & mingw32-g++ -std=c+11 -c main.cpp -o main.o (which is what CB is using according to the output log)

If you have e better example than stoi(), please do make it simple, and add full text of try/catch.

EDIT: This is written at the top of my file:

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <algorithm>
#include <stdexcept>
using namespace std;

Error message refers to:

int player;
string input;

cin >> input;
player = stoi(input)

My Code::Blocks compiler outputs this log:

mingw32-g++.exe -std=c++11  -c "C:\Users\<name>\#C++\main.cpp" -o "C:\Users
<name>\#C++\main.o"
mingw32-g++.exe  -o "C:\Users\<name>\#C++\main.exe" "C:\Users
<name>\#C++\main.o"   
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Source code, as requested: removed as it is not related to the solution.

theusual
  • 69
  • 1
  • 2
  • 9
  • 1
    Did you `#include `? Did you prefix it with the correct scope `std::stoi`? – Cory Kramer Jun 05 '15 at 11:27
  • @CoryKramer I've included a bunch of libraries, including . I'm also using `using namespace std;`. It runs fine in Code::Blocks and everything is working as it should. But not through command line. – theusual Jun 05 '15 at 11:30
  • Please do not post *"Why isn't my code working"* questions without [SSCCE](http://www.sscce.org) and all information necessary to reproduce the problem. – Baum mit Augen Jun 05 '15 at 11:30
  • @BaummitAugen I'm sorry, not used to stackoverflow. Edited my post. – theusual Jun 05 '15 at 11:36
  • @theusual, if the code isn't super long (which I presume it's not), could you paste *all* the source? – erip Jun 05 '15 at 11:52
  • @erip Source code is added. – theusual Jun 05 '15 at 12:00
  • What version of mingw32 are you using? See this discussion http://stackoverflow.com/questions/14590410/stoi-and-stoll-in-c as well as some of the comments in this discussion http://stackoverflow.com/questions/29088327/stoi-was-not-declared-in-this-scope-despite-using-c11 as well as the discussion in this forum https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015 – Richard Chambers Jun 05 '15 at 12:34
  • @RichardChambers I used a bat script from this site: http://forums.codeblocks.org/index.php?topic=9054.0 and it gave me this output: `#define __MINGW32_VERSION 3.20` and `#define __W32API_VERSION 3.17`. Checking your links now. – theusual Jun 05 '15 at 13:20
  • CORRECTION: Mingw32 path -> `C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.7.1` I guess it's v4.7.1? – theusual Jun 05 '15 at 13:34

1 Answers1

0

I managed to get it to work! I downloaded the latest Mingw32 release (GCC 5.1.0 as this is written) from: http://nuwen.net/mingw.html.

I changed my Environment variable to <your path>\MinGW\bin and ran g++ -std=c++11 -static main.cpp -o test.exe. Worked like a charm. Still don't know how C::B could do it, while it didn't work through the command-line, even though they pointed to the same folder.

theusual
  • 69
  • 1
  • 2
  • 9