C++ Problem The Math Game Program Requirements Generate simple math fact problem
ID: 3564853 • Letter: C
Question
C++ Problem
The Math Game Program Requirements Generate simple math fact problems: 1. a. Addition (the total must be an integer 0) b. Subtraction (the difference must be an integer 0) c. Multiplication (the product must be an integer 0) d. Division (the quotient must be an integer 0) 2. Validate user input at every opportunity 3. The program should keep track of the following statistics: a. The user's name b. The total correct answers C. The total wrong answers d. The total earnings ($0.05 is awarded for every correct response and $0.03 is subtracted from every incorrect response) 4. A separate text file must be created for every user: a. Statistics are read from the file at the start of the game (if the user is a returning player) b. Statistics are recorded at the end of every game 5. The program must be developed using functions so that the main function consists mostly offunction calls. Below are just a few of the required functions: a. credits //your name and what the program does b. menu //display menu with various options validateUserResponse //to validate user input at all stages of the game checkUserAnswer given a math problem, this function is used to check if the answer th is correct/ incorrect e. updatestats used to keep a running total of game statistics f. displayStats display statistics on screen g. retirevestats retrieve player statistics from external txt file h. saveStats e player statistics on external txt file i. you may also want to consider the following: generateAddition, generatesubtraction, generateMultiplication and generateDivision use these to generate a problem of the appropriate typeExplanation / Answer
#include<iostream>
#include <cstdlib>
#include <fstream>
char name[50];
int totalCorrect=0,totalWrong=0;
double earning=0.0;
bool credits();
bool checkUserAnswer(double answer,double result);
char menu();
void updateStats(bool correct);
void displayStats();
double generateAddition(double a,double b);
double generateSubstraction(double a,double b);
double generateMultiplication(double a,double b);
double generateDivision(double a,double b);
void saveStats();
int main()
{
char ch;
if(credits()) {
while(true) {
ch = menu();
}
} else {
cout<<"Terminated ";
}
return 0;
}
bool credits()
{
char ch;
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"***** TheMathGame ******* ";
cout<<"***** By Prof. ******* ";
cout<<"***** Flores ******* ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"y/Y to continue , or any other char to exit ";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"Enter your name and press <ENTER> ";
cin>>name;
return true;
} else {
return false;
}
}
char menu()
{
char ch;
bool correct;
cout<<"*****CHOOSE A PROBLEM****** ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"***** 1. ADD ******* ";
cout<<"***** 2. SUBSTRACT ******* ";
cout<<"***** 3. MULTIPLY ******* ";
cout<<"***** 4. DIVIDE ******* ";
cout<<"***** 5. STATS ******* ";
cout<<"***** 6. SAVE STATS TO FILE ******* ";
cout<<"***** n/N to QUIT ******* ";
cout<<"*************************** ";
cout<<"*************************** ";
cin>>ch;
int a;
int b;
do {
a = rand() % 10+1;
b = rand() % 10+1;
} while(a<b);
double result,answer;
do {
switch(ch) {
case '1':
result = a+b;
answer = generateAddition(a,b);
correct=checkUserAnswer(answer,result);
updateStats(correct);
break;
case '2':
result = a-b;
answer=generateSubstraction(a,b);
correct=checkUserAnswer(answer,result);
updateStats(correct);
break;
case '3':
result = a*b;
answer=generateMultiplication(a,b);
correct=checkUserAnswer(answer,result);
updateStats(correct);
break;
case '4':
result = a/b;
answer=generateDivision(a,b);
correct=checkUserAnswer(answer,result);
updateStats(correct);
break;
case '5':
displayStats();
break;
case '6':
saveStats();
break;
case 'n':
break;
case 'N':
break;
default:
cout<<"Incorrect Choice ";
}
} while(ch=='n'||ch=='N');
return ch;
}
bool checkUserAnswer(double answer,double result)
{
if(answer == result) {
cout<<"***********RIGHT!*********** ";
return true;
} else {
cout<<"***********WRONG!*********** ";
return false;
}
}
void updateStats(bool correct)
{
if(correct)
{
totalCorrect++;
earning = earning + 0.05;
} else {
totalWrong++;
earning = earning - 0.03;
}
}
void displayStats()
{
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"***** "<<name<<" ******* ";
cout<<"***Total Earnings "<<earning<<"** ";
cout<<"***Total Correct "<<totalCorrect<<"** ";
cout<<"***Total Wrong "<<totalWrong<<"** ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*************************** ";
}
double generateAddition(double a,double b)
{
double answer;
cout<<"*******ADDITION************ ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*** "<<a<<" + "<<b<< " =? ***** ";
cout<<"*************************** ";
cout<<"*************************** ";
cin>>answer;
return answer;
}
double generateSubstraction(double a,double b)
{
double answer;
cout<<"*******SUBSTRACTION********* ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*** "<<a<<" - "<<b<< " =? ***** ";
cout<<"*************************** ";
cout<<"*************************** ";
cin>>answer;
return answer;
}
double generateMultiplication(double a,double b)
{
double answer;
cout<<"*****MULTIPLICATION******** ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*** "<<a<<" * "<<b<< " =? ***** ";
cout<<"*************************** ";
cout<<"*************************** ";
cin>>answer;
return answer;
}
double generateDivision(double a,double b)
{
double answer;
cout<<"*******DIVISION*********** ";
cout<<"*************************** ";
cout<<"*************************** ";
cout<<"*** "<<a<<" / "<<b<< " =? ***** ";
cout<<"*************************** ";
cout<<"*************************** ";
cin>>answer;
return answer;
}
void saveStats()
{
ofstream myfile;
myfile.open ("stats.txt");
myfile << "Writing this to a file. ";
myfile<<"*************************** ";
myfile<<"*************************** ";
myfile<<"*************************** ";
myfile<<"***** "<<name<<" ******* ";
myfile<<"***Total Earnings "<<earning<<"** ";
myfile<<"***Total Correct "<<totalCorrect<<"** ";
myfile<<"***Total Wrong "<<totalWrong<<"** ";
myfile<<"*************************** ";
myfile<<"*************************** ";
myfile<<"*************************** ";
myfile.close();
cout<<"Saved Stats to stats.txt successfully ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.