Reputation: 69
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.
Upvotes: 1
Views: 12158
Reputation: 69
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.
Upvotes: 0