Hello Below is a working code for math game in C We need to

Hello,

Below is a working code for math game in C++

We need to modify the following code to use REFERENCE PARAMETERS instead of pointer (*) variables.

===========================================================

==============================================================

Solution

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<fstream>

using namespace std;

bool credits(char name[]);
bool checkUserAnswer(double answer,double result);

char menu(int &totalCorrect,int &totalWrong,double &earning,char name[]); //NEEDS TO BE CHANGED TO USE REFERENCE PARAMETERS

void updateStats(bool correct,int &totalCorrect,int &totalWrong,double &earning); //NEEDS TO BE CHANGED TO USE REFERENCE PARAMETERS

void displayStats(int &totalCorrect,int &totalWrong,double &earning,char name[]); //NEEDS TO BE CHANGED TO USE REFERENCE PARAMETERS

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 &totalCorrect,int &totalWrong,double &earning,char name[]); //NEEDS TO BE CHANGED TO USE REFERENCE PARAMETERS

int main()
{

char ch;
char name[50];
int totalCorrect=0,totalWrong=0;
double earning=0.0;

if(credits(name))
{
while(true)
{
ch = menu(totalCorrect,totalWrong,earning,name);
if(ch==\'n\'||ch==\'N\')
break;
}
}
else
{
cout<<\"See you!\ \";
}
return 0;
}

bool credits(char name[])
{
char ch;
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"**** ****\ \";
cout<<\"**** The Math Game ****\ \";
cout<<\"**** by Timur Kabilov ****\ \";
cout<<\"**** ****\ \";
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(int &totalCorrect,int &totalWrong,double &earning,char name[])
{
char ch;
bool correct;

cout<<\"***************************\ \";
cout<<\"**** CHOOSE A PROBLEM ****\ \";
cout<<\"***************************\ \";
cout<<\"*** (1) ADD ******\ \";
cout<<\"*** (2) SUBSTRACT ******\ \";
cout<<\"*** (3) MULTIPLY ******\ \";
cout<<\"*** (4) DIVIDE ******\ \";
cout<<\"*** (5) STATS ******\ \";
cout<<\"*** (6) SAVE TO FILE ******\ \";
cout<<\"*** ******\ \";
cout<<\"*** N/n to QUITE ******\ \";
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,totalCorrect,totalWrong,earning);
break;

case \'2\':
result = a-b;
answer=generateSubstraction(a,b);
correct=checkUserAnswer(answer,result);
updateStats(correct,totalCorrect,totalWrong,earning);
break;
case \'3\':
result = a*b;
answer=generateMultiplication(a,b);
correct=checkUserAnswer(answer,result);
updateStats(correct,totalCorrect,totalWrong,earning);
break;

case \'4\':
result = a/b;
answer=generateDivision(a,b);
correct=checkUserAnswer(answer,result);
updateStats(correct,totalCorrect,totalWrong,earning);
break;
case \'5\':
displayStats(totalCorrect,totalWrong,earning,name);
break;
case \'6\':
saveStats(totalCorrect,totalWrong,earning,name);
break;
case \'n\':
cout<<\"See you!\ \";
return \'n\';
case \'N\':
cout<<\"See you!\ \";
return \'N\';
default:
cout<<\"Invalid Option, please enter a option!\ \";
cout<<\"\ \";
}

}while(ch==\'n\'||ch==\'N\');

return 0;
}

bool checkUserAnswer(double answer,double result)
{
if(answer == result)
{
cout<<\"********* RIGHT! **********\ \";
cout<<\"\ \";
return true;
}
else
{
cout<<\"********* WRONG! **********\ \";
cout<<\"\ \";
return false;
}
}

void updateStats(bool correct,int &totalCorrect,int &totalWrong,double &earning)
{
if(correct)
{
totalCorrect = totalCorrect+1 ;
earning = earning + 0.05;
} else {
totalWrong = totalWrong +1;
earning = earning - 0.03;
}
}

void displayStats(int &totalCorrect,int &totalWrong,double &earning,char name[])
{
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"********* \"<<name<<\" ***********\ \";
cout<<\"*** Total Earnings: \"<<earning<<\" ***\ \";
cout<<\"*** Total Correct: \"<<totalCorrect<<\" ***\ \";
cout<<\"*** Total Wrong: \"<<totalWrong<<\" ***\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"\ \";
}

double generateAddition(double a,double b)
{
double answer;
cout<<\"******** ADDITION *********\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"****** \"<<a<<\" + \"<<b<< \" =? ******\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cin>>answer;
while (!cin.good())
{
cin.clear();
cin.ignore(100000, \'\ \');
cout<<\"That\'s not a number, please enter a number!\ \";
cin>>answer;
}
return answer;
}

double generateSubstraction(double a,double b)
{
double answer;
cout<<\"****** SUBSTRACTION *******\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"****** \"<<a<<\" - \"<<b<< \" =? *******\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cin>>answer;
while (!cin.good())
{
cin.clear();
cin.ignore(100000, \'\ \');
cout<<\"That\'s not a number, please enter a number!\ \";
cin>>answer;
}
return answer;
}

double generateMultiplication(double a,double b)
{
double answer;
cout<<\"**** MULTIPLICATION *******\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"****** \"<<a<<\" * \"<<b<< \" =? ********\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cin>>answer;
while (!cin.good())
{
cin.clear();
cin.ignore(100000, \'\ \');
cout<<\"That\'s not a number, please enter a number!\ \";
cin>>answer;
}
return answer;
}

double generateDivision(double a,double b)
{
double answer;
cout<<\"******** DIVISION *********\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cout<<\"****** \"<<a<<\" / \"<<b<< \" =? *******\ \";
cout<<\"***************************\ \";
cout<<\"***************************\ \";
cin>>answer;
while (!cin.good())
{
cin.clear();
cin.ignore(100000, \'\ \');
cout<<\"That\'s not a number, please enter a number!\ \";
cin>>answer;
}
return answer;
}

void saveStats(int &totalCorrect,int &totalWrong,double &earning,char name[])
{
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 to stats.txt successfully\ \";
}

-----------------------------------------------------------------------------------------

Hello, Below is a working code for math game in C++ We need to modify the following code to use REFERENCE PARAMETERS instead of pointer (*) variables. =========
Hello, Below is a working code for math game in C++ We need to modify the following code to use REFERENCE PARAMETERS instead of pointer (*) variables. =========
Hello, Below is a working code for math game in C++ We need to modify the following code to use REFERENCE PARAMETERS instead of pointer (*) variables. =========
Hello, Below is a working code for math game in C++ We need to modify the following code to use REFERENCE PARAMETERS instead of pointer (*) variables. =========
Hello, Below is a working code for math game in C++ We need to modify the following code to use REFERENCE PARAMETERS instead of pointer (*) variables. =========
Hello, Below is a working code for math game in C++ We need to modify the following code to use REFERENCE PARAMETERS instead of pointer (*) variables. =========

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site