C++ Need Help! Hello I get this error message when I use stoi to validate the us
ID: 3662841 • Letter: C
Question
C++ Need Help!
Hello I get this error message when I use stoi to validate the user input (I want only integer no letters, no special character, no whitespace, no empty input).
And I have no clue what it's saying.
Please help.
This is what I have so far.(I bolded out the part where I have a problem with).
Thank you very much. I appreciate it!!
#include // limits of named constants, see page 1284
#include //math library
#include // library for strings and other useful validations
#include // library for manipulating output
#include // library to direct output to files and get input from files
#include // library for the rand and srand functions
#include // library to work with time
#include //string library
#include //to be able to use strlen
#define NDEBUG
#include
#include // ----typeid(varName).name()
#include
using namespace std;
//void generateDivision(double totalWages, int totalCorrect, int totalWrong); Wrong
void generateAddition(double& totalWages, int& totalCorrect, int& totalWrong);
void generateSubtraction(double& totalWages, int& totalCorrect, int& totalWrong);
void generateMultiplication(double& totalWages, int& totalCorrect, int& totalWrong);
void generateDivision(double& totalWages, int& totalCorrect, int& totalWrong);
void menu();
void checkTheAnswwer(string theUserResponse, int theCorrectAnswer, double& totalWages, int& totalCorrect, int& totalWrong);
void stats(string userName, int totalCorrect, int totalIncorrect, double totalWages);
int main()
{
string userInput = "";
double totalWages = 0;
int totalCorrect = 0;
int totalWrong = 0;
string userName;
string go = "y";
int userInputlength = 0;
ifstream inData;
ofstream outData;
char continu = 'y';
//splash
cout
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "********************** **********************" << endl
<< "********************** **********************" << endl
<< "********************** The Math Game **********************" << endl
<< "********************** By **********************" << endl
<< "********************** asdasdsasasadas **********************" << endl
<< "********************** **********************" << endl
<< "********************** **********************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl;
cout
<< " Do you want to play a game? Press Y/y to continue, any other char to exit. "
<< endl;
getline(cin, userInput);
while (userInput == "y" || userInput == "Y")
{
int userNamelength=0;
char ch=' ';
system("cls");
while (1)
{
cout << "Enter your name: ";
getline(cin, userName);
bool Reject = false;
if (userName.empty())
continue;
for (unsigned int i = 0; i < userName.length() && !Reject; i++)
{
if (isalpha(userName[i]))
continue;
Reject = true;
}
if (!Reject)
break;
}
cout << " Hi " << userName << endl;
cout << " ";
while (continu == 'y' || continu == 'Y')
{
string userFileName = userName + ".txt";
inData.open(userFileName);
if (!inData.is_open())
{
//create userFile for output. remember that when you issue the command outData.open(userFileName.c_str());
//if the file does not exist, it will get created.
outData.open(userFileName.c_str());
cout << "Welcome " << userName << " I notice that this is the first time you play the game ";
//we store values in the file to prevent an exception if we attempt to read data from an empty file
/* outData << userName << endl;
outData << totalCorrect << endl;
outData << totalWrong << endl;
outData << totalWages << endl;*/
cout << "A text file with your name was created ";
cout << endl;
system("pause");
break;
}
else //the file exists
{
//you may proceed to read statistics
//userName; totalCorrect, totalWrong, totalEarnings
//cin>>userName;
inData >> userName;
inData >> totalCorrect;
inData >> totalWrong;
inData >> totalWages;
//close file once you read the data
inData.close();
//outData.open(userFileName.c_str());
cout << "welcome " << userName << " I notice that you have played this game before ";
cout << " The statistics from the previous game were retrieved and loaded ";
cout << endl;
system("pause");
break;
}
}
while (go == "y")
{
menu();
getline(cin, userInput);
if (userInput == "1")
{
generateAddition(totalWages, totalCorrect, totalWrong);
}
if (userInput == "2")
{
generateSubtraction(totalWages, totalCorrect, totalWrong);
}
if (userInput == "3")
{
generateMultiplication(totalWages, totalCorrect, totalWrong);
}
if (userInput == "4")
{
generateDivision(totalWages, totalCorrect, totalWrong);
}
if (userInput == "5")
{
stats(userName, totalCorrect, totalWrong, totalWages);
}
if (userInput=="6")
{
outData << userName << endl;
outData << totalCorrect << endl;
outData << totalWrong << endl;
outData << totalWages << endl;
outData.close();
system("pause");
return 0;
}
}
system("pause");
return 0;
}
while (userInput != "y" || userInput != "Y")
{
system("cls");
cout << "Bye Bye" << endl;
system("pause");
return 0;
}
}
void generateAddition(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand()%10+1;
unsigned int num2 = rand()%10+1;
unsigned int theCorrectAnswer = num1 + num2;
cout << "**************** Addition *****************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " < cout << "*************************************************" << endl;
cout << "Your Answer = " << endl;
getline(cin, theUserResponse);
cout<< " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void generateSubtraction(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand() % 10 + 1;
unsigned int num2 = rand() % 10 + 1;
unsigned int theCorrectAnswer = num1+num2-num2;
cout << "************** Subtraction ***************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " << num1+num2 << " - " << num2 << " = ?"<< endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " Your Answer = " << endl;
getline(cin, theUserResponse);
cout << " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void generateMultiplication(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand() % 10 + 1;
unsigned int num2 = rand() % 10 + 1;
unsigned int theCorrectAnswer = num1 * num2;
cout << "************* Multiplication **************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " << num1 << " X " << num2 << " = ?" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << "Your Answer = " << endl;
getline(cin, theUserResponse);
cout << " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void generateDivision(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand() % 10 + 1;
unsigned int num2 = rand() % 10 + 1;
unsigned int theCorrectAnswer = num1;
cout << "**************** Division *****************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " << num1*num2 << " / " << num2 << " = ?" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << "Your Answer = " << endl;
getline(cin, theUserResponse);
cout << " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void menu()
{
system("cls");
//cout << "Hi " << userName;
cout
<< "****************** Choose your options *******************" << endl
<< "************************************************************" << endl
<< "************************************************************" << endl
<< "****************** 1. addition *******************" << endl
<< "****************** 2. subtraction *******************" << endl
<< "****************** 3. multiplication *******************" << endl
<< "****************** 4. division *******************" << endl
<< "****************** 5. stats *******************" << endl
<< "****************** 6. quit *******************" << endl
<< "************************************************************" << endl
<< "************************************************************" << endl
<< "************************************************************" << endl;
}
//two passing by value and three pass by reference
void checkTheAnswwer(string theUserResponse, int theCorrectAnswer, double& totalWages, int& totalCorrect, int& totalWrong)
{
int userAnswer = stoi(theUserResponse);//stoi is a function that turns string into the integer.
if (userAnswer == theCorrectAnswer)
{
cout << "******************** Good Job! ********************" << endl;
totalCorrect++;
//totalWages=totalWages+0.05;
totalWages += 0.05;
}
else
{
cout << "******************** Too Bad! ********************" << endl;
totalWrong++;
//totalWages=totalWages-0.05;
totalWages -= 0.03;
}
}
void stats(string userName, int totalCorrect, int totalIncorrect, double totalWages)
{
system("cls");
cout << "******************** STATISTIC ********************" << endl;
cout << "***********************************************************" << endl;
cout << "***********************************************************" << endl;
cout << " User Name: " << userName << endl;
cout << " Total Correct: " << totalCorrect << endl;
cout << " Total Incorrect: " << totalIncorrect << endl;
cout << " Total wages: $" << totalWages << endl;
cout << "***********************************************************" << endl;
cout << "***********************************************************" << endl;
cout << "***********************************************************" << endl;
system("pause");
system("cls");//clears the screen
}
Explanation / Answer
#include<limits.h> // limits of named constants, see page 1284
#include<math.h> //math library
#include<string> // library for strings and other useful validations
#include<iostream> // library for manipulating output
#include<fstream>// library to direct output to files and get input from files
#include<stdlib.h>// library for the rand and srand functions
#include<time.h> // library to work with time
#include<cstring> //string library
#define NDEBUG
using namespace std;
//void generateDivision(double totalWages, int totalCorrect, int totalWrong); Wrong
void generateAddition(double& totalWages, int& totalCorrect, int& totalWrong);
void generateSubtraction(double& totalWages, int& totalCorrect, int& totalWrong);
void generateMultiplication(double& totalWages, int& totalCorrect, int& totalWrong);
void generateDivision(double& totalWages, int& totalCorrect, int& totalWrong);
void menu();
void checkTheAnswwer(string theUserResponse, int theCorrectAnswer, double& totalWages, int& totalCorrect, int& totalWrong);
void stats(string userName, int totalCorrect, int totalIncorrect, double totalWages);
int main()
{
string userInput = "";
double totalWages = 0;
int totalCorrect = 0;
int totalWrong = 0;
string userName;
string go = "y";
int userInputlength = 0;
ofstream outData;
char continu = 'y';
//splash
cout
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "********************** **********************" << endl
<< "********************** **********************" << endl
<< "********************** The Math Game **********************" << endl
<< "********************** By **********************" << endl
<< "********************** asdasdsasasadas **********************" << endl
<< "********************** **********************" << endl
<< "********************** **********************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl
<< "***********************************************************" << endl;
cout
<< " Do you want to play a game? Press Y/y to continue, any other char to exit. "
<< endl;
getline(cin, userInput);
while (userInput == "y" || userInput == "Y")
{
int userNamelength=0;
char ch=' ';
system("cls");
while (1)
{
cout << "Enter your name: ";
getline(cin, userName);
bool Reject = false;
if (userName.empty())
continue;
for (unsigned int i = 0; i < userName.length() && !Reject; i++)
{
if (isalpha(userName[i]))
continue;
Reject = true;
}
if (!Reject)
break;
}
cout << " Hi " << userName << endl;
cout << " ";
while (continu == 'y' || continu == 'Y')
{
string userFileName = userName + ".txt";
ifstream inData(userFileName, std::ifstream::in);
//ifstream myfile ("example.txt");
if (!inData.is_open())
{
//create userFile for output. remember that when you issue the command outData.open(userFileName.c_str());
//if the file does not exist, it will get created.
outData.open(userFileName.c_str());
cout << "Welcome " << userName << " I notice that this is the first time you play the game ";
//we store values in the file to prevent an exception if we attempt to read data from an empty file
/* outData << userName << endl;
outData << totalCorrect << endl;
outData << totalWrong << endl;
outData << totalWages << endl;*/
cout << "A text file with your name was created ";
cout << endl;
system("pause");
break;
}
else //the file exists
{
//you may proceed to read statistics
//userName; totalCorrect, totalWrong, totalEarnings
//cin>>userName;
inData >> userName;
inData >> totalCorrect;
inData >> totalWrong;
inData >> totalWages;
//close file once you read the data
inData.close();
//outData.open(userFileName.c_str());
cout << "welcome " << userName << " I notice that you have played this game before ";
cout << " The statistics from the previous game were retrieved and loaded ";
cout << endl;
system("pause");
break;
}
}
while (go == "y")
{
menu();
getline(cin, userInput);
if (userInput == "1")
{
generateAddition(totalWages, totalCorrect, totalWrong);
}
if (userInput == "2")
{
generateSubtraction(totalWages, totalCorrect, totalWrong);
}
if (userInput == "3")
{
generateMultiplication(totalWages, totalCorrect, totalWrong);
}
if (userInput == "4")
{
generateDivision(totalWages, totalCorrect, totalWrong);
}
if (userInput == "5")
{
stats(userName, totalCorrect, totalWrong, totalWages);
}
if (userInput=="6")
{
outData << userName << endl;
outData << totalCorrect << endl;
outData << totalWrong << endl;
outData << totalWages << endl;
outData.close();
system("pause");
return 0;
}
}
system("pause");
return 0;
}
while (userInput != "y" || userInput != "Y")
{
system("cls");
cout << "Bye Bye" << endl;
system("pause");
return 0;
}
}
void generateAddition(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand()%10+1;
unsigned int num2 = rand()%10+1;
unsigned int theCorrectAnswer = num1 + num2;
cout << "**************** Addition *****************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " < cout << "*************************************************" << endl;
cout << "Your Answer = " << endl;
getline(cin, theUserResponse);
cout<< " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void generateSubtraction(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand() % 10 + 1;
unsigned int num2 = rand() % 10 + 1;
unsigned int theCorrectAnswer = num1+num2-num2;
cout << "************** Subtraction ***************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " << num1+num2 << " - " << num2 << " = ?"<< endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " Your Answer = " << endl;
getline(cin, theUserResponse);
cout << " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void generateMultiplication(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand() % 10 + 1;
unsigned int num2 = rand() % 10 + 1;
unsigned int theCorrectAnswer = num1 * num2;
cout << "************* Multiplication **************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " << num1 << " X " << num2 << " = ?" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << "Your Answer = " << endl;
getline(cin, theUserResponse);
cout << " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void generateDivision(double& totalWages, int& totalCorrect, int& totalWrong)
{
string theUserResponse;
system("cls");
srand(time(NULL));
unsigned int num1 = rand() % 10 + 1;
unsigned int num2 = rand() % 10 + 1;
unsigned int theCorrectAnswer = num1;
cout << "**************** Division *****************" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << " What is " << num1*num2 << " / " << num2 << " = ?" << endl;
cout << "*************************************************" << endl;
cout << "*************************************************" << endl;
cout << "Your Answer = " << endl;
getline(cin, theUserResponse);
cout << " ";
//enter an answer
//validate the userAnswer
system("cls");
checkTheAnswwer(theUserResponse, theCorrectAnswer, totalWages, totalCorrect, totalWrong);
system("pause");
}
void menu()
{
system("cls");
//cout << "Hi " << userName;
cout
<< "****************** Choose your options *******************" << endl
<< "************************************************************" << endl
<< "************************************************************" << endl
<< "****************** 1. addition *******************" << endl
<< "****************** 2. subtraction *******************" << endl
<< "****************** 3. multiplication *******************" << endl
<< "****************** 4. division *******************" << endl
<< "****************** 5. stats *******************" << endl
<< "****************** 6. quit *******************" << endl
<< "************************************************************" << endl
<< "************************************************************" << endl
<< "************************************************************" << endl;
}
//two passing by value and three pass by reference
void checkTheAnswwer(string theUserResponse, int theCorrectAnswer, double& totalWages, int& totalCorrect, int& totalWrong)
{
int userAnswer = stoi(theUserResponse);//stoi is a function that turns string into the integer.
if (userAnswer == theCorrectAnswer)
{
cout << "******************** Good Job! ********************" << endl;
totalCorrect++;
//totalWages=totalWages+0.05;
totalWages += 0.05;
}
else
{
cout << "******************** Too Bad! ********************" << endl;
totalWrong++;
//totalWages=totalWages-0.05;
totalWages -= 0.03;
}
}
void stats(string userName, int totalCorrect, int totalIncorrect, double totalWages)
{
system("cls");
cout << "******************** STATISTIC ********************" << endl;
cout << "***********************************************************" << endl;
cout << "***********************************************************" << endl;
cout << " User Name: " << userName << endl;
cout << " Total Correct: " << totalCorrect << endl;
cout << " Total Incorrect: " << totalIncorrect << endl;
cout << " Total wages: $" << totalWages << endl;
cout << "***********************************************************" << endl;
cout << "***********************************************************" << endl;
cout << "***********************************************************" << endl;
system("pause");
system("cls");//clears the screen
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.