I am having an issue trying to convert a number into a string. The purpose is for error checking to make sure the number is of a specific length. I have tried using both to_string() and convert.str() functions but get the same error back when trying to compile. I am using MinGw g++ to compile and realize I need to tell it I want the C++11 standard, which I believe I have done. My compiler code is as follows:
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).exe"
cmd /c $(NAME_PART).exe
Now assuming that is correct, my code for using to_string() is as follows:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
string code = to_string(book_code);
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, '\n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
And my code for using convert.str() is as follows:
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
ostringstream Convert;
convert << book_code;
string code = convert.str();
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, '\n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
Neither of these was successful and both returned "error: 'to_string' was not declared in this scope"
Am I missing something obvious?
Aucun commentaire:
Enregistrer un commentaire