dont use global variables i cannot get the name in stats plz help me fix the pro
ID: 663711 • Letter: D
Question
dont use global variables i cannot get the name in stats plz help me fix the program #include<iostream>
#include<cmath>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;
void displayStats(int *totalCorrect, int *totalWrong, double *earing, string name);
void updateStates(bool correct, int *totalCorrect, int *totalWorng, double *earing);
bool credits(string name);
bool checkUserAnswer(double answer, double result);
char menu(int *totalCorrect, int *totalWrong, double *earing, string name);
double generateAddition(double number1, double number2);
double generateSubstraction(double number1, double number2);
double generateMultiplication(double number1, double number2);
double generateDivision(double number1, double number2);
void saveStats(int *totalCorrect, int *totalWrong, double *earing, string name);
int main()
{ char ch;
string name;
int totalCorrect = 0 ;
int 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 << " GOOD BYE !" << endl;
cout << endl;
}
return 0;
}
// Math Game //
bool credits(string name)
{ char ch;
cout << "*******************" << endl;
cout << "*******************" << endl;
cout << "*** Math Game *****" << endl;
cout << "*** Chung An *****" << endl;
cout << "*** Chen *****" << endl;
cout << "*******************" << endl;
cout << "*******************" << endl;
cout << "y/Y to contiune, or any other char to exit" << endl;
cin >> ch;
cin.clear();
cin.ignore(1000000, ' ');
if (ch == 'y' || ch == 'Y')
{ cout << " Enter your Name and press Enter:" << endl;
getline(cin,name);
if (!name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")) { while(!name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")) { cout << " enter a name plz: " << endl;
cin.clear();
getline(cin,name);
}
}
else if (name.empty())
{ while(name.empty())
{ cout << " enter a name" << endl;
cin.clear();
getline(cin,name);
}
}
else
{ //cin >> name;
return true;
}
}
else
{ return false;
}
}
// Menu
char menu(int *totalCorrect, int *totalWrong, double *earing, string name1 )
{ char ch;
bool correct;
cout << " Choose a problem" << endl;
cout << " ****************" << endl;
cout << " 1. ADD " << endl;
cout << " 2. SUBSTRACT " << endl;
cout << " 3. MULTIPLY " << endl;
cout << " 4. DIVIDE " << endl;
cout << " 5. STATS " << endl;
cout << " 6. SAVE " << endl;
cout << " n/N to QUIT " << endl;
cout << " ****************" << endl;
cin >> ch;
int number1,number2;
do
{ number1 = rand()%10+1;
number2 = rand()%10+1;
}
while(number1<number2);
double result, answer;
do
{ switch(ch)
{ case '1':
result = number1+number2;
answer = generateAddition(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '2':
result = number1-number2;
answer = generateSubstraction(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '3':
result = number1*number2;
answer = generateMultiplication(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '4':
result = number1/number2;
answer = generateDivision(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '5':
displayStats(totalCorrect,totalWrong,earing, name1);
break;
case '6':
saveStats(totalCorrect,totalWrong,earing, name1);
break;
case 'n':
cout << " good bye" << endl;
return 'n';
case 'N':
cout << " good bye" << endl;
return 'N';
}
}while (ch=='n'|| ch =='N');
return 0;
}
// check answers
bool checkUserAnswer(double answer, double result)
{ if (answer = result)
{ cout << " RIGHT" << endl;
cout << endl;
return true;
}
else
{ cout << " WRONG" << endl;
cout << endl;
return false;
}
}
// update
void updateStates(bool correct, int *totalCorrect, int *totalWrong, double *earing)
{ if (correct)
{ *totalCorrect = *totalCorrect+1;
*earing = *earing + 0.05;
}
else
{ *totalWrong = *totalWrong +1;
*earing = *earing -0.03;
}
}
// display
void displayStats(int *totalCorrect, int *totalWrong, double *earing, string name)
{ cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "** " << name << " **" << endl;
cout << " Total Earing: " << *earing << endl;
cout << " Total Correct: " << *totalCorrect << endl;
cout << " Total Wrong: " << *totalWrong << endl;
cout << "***********************" << endl;
}
// generateAddition
double generateAddition(double number1, double number2)
{ double answer;
cout << " ADDITION " << endl;
cout << number1 << " + " << number2 <<" = ? " << endl;
//getline(cin,answer);
cin >> answer;
while (!cin.good())
{ cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
// generateSubstraction
double generateSubstraction(double number1, double number2)
{ double answer;
cout << " SUBSTRACTION " << endl;
cout << number1 << " - " << number2 << " = ?" << endl;
cin >> answer;
while (!cin.good())
{ cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
//Multiplication
double generateMultiplication(double number1, double number2)
{ double answer;
cout << " MULTIPLICATION " << endl;
cout << number1 << " * " << number2 << " = ?" << endl;
cin >> answer;
while (!cin.good())
{ cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
// DIVISION
double generateDivision(double number1, double number2)
{ double answer;
cout << " DIVISION" << endl;
cout << number1 << " / " << number2 << " = ?" << endl;
cin >> answer;
while (!cin.good())
{ cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
// SAVE
void saveStats(int *totalCorrect, int *totalWrong, double *earing, string name)
{ ofstream myfile;
myfile.open ( "stats.txt");
myfile << "***************" << endl;
myfile << name << endl;
myfile << "Total Correct: " << *totalCorrect << endl;
myfile << "Total Wrong: " << *totalWrong << endl;
myfile << "Total earing: " << *earing << endl;
myfile << "***************" << endl;
myfile.close();
}
Explanation / Answer
You need to pass a pointer for its value to be changed inside a function
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;
void displayStats(int *totalCorrect, int *totalWrong, double *earing, string name);
void updateStates(bool correct, int *totalCorrect, int *totalWorng, double *earing);
bool credits(string &name);
bool checkUserAnswer(double answer, double result);
char menu(int *totalCorrect, int *totalWrong, double *earing, string name);
double generateAddition(double number1, double number2);
double generateSubstraction(double number1, double number2);
double generateMultiplication(double number1, double number2);
double generateDivision(double number1, double number2);
void saveStats(int *totalCorrect, int *totalWrong, double *earing, string name);
int main()
{
char ch;
string name;
int totalCorrect = 0 ;
int 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 << " GOOD BYE !" << endl;
cout << endl;
}
return 0;
}
// Math Game //
bool credits(string &name)
{
char ch;
cout << "*******************" << endl;
cout << "*******************" << endl;
cout << "*** Math Game *****" << endl;
cout << "*** Chung An *****" << endl;
cout << "*** Chen *****" << endl;
cout << "*******************" << endl;
cout << "*******************" << endl;
cout << "y/Y to contiune, or any other char to exit" << endl;
string namePlacer;
cin >> ch;
cin.clear();
cin.ignore(1000000, ' ');
if (ch == 'y' || ch == 'Y')
{
cout << " Enter your Name and press Enter:" << endl;
getline(cin,namePlacer);
if (namePlacer.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") != namePlacer.npos || namePlacer.empty())
{
while(namePlacer.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890") != namePlacer.npos || namePlacer.empty())
{
cout << " enter a name plz: " << endl;
cin.clear();
getline(cin,namePlacer);
}
}
name = namePlacer;
return true;
}
else
{
return false;
}
}
// Menu
char menu(int *totalCorrect, int *totalWrong, double *earing, string name1 )
{
char ch;
bool correct;
cout << " Choose a problem" << endl;
cout << " ****************" << endl;
cout << " 1. ADD " << endl;
cout << " 2. SUBSTRACT " << endl;
cout << " 3. MULTIPLY " << endl;
cout << " 4. DIVIDE " << endl;
cout << " 5. STATS " << endl;
cout << " 6. SAVE " << endl;
cout << " n/N to QUIT " << endl;
cout << " ****************" << endl;
cin >> ch;
int number1,number2;
do
{
number1 = rand()%10+1;
number2 = rand()%10+1;
}
while(number1<number2);
double result, answer;
do
{
switch(ch)
{
case '1':
result = number1+number2;
answer = generateAddition(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '2':
result = number1-number2;
answer = generateSubstraction(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '3':
result = number1*number2;
answer = generateMultiplication(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '4':
result = number1/number2;
answer = generateDivision(number1,number2);
correct = checkUserAnswer(answer,result);
updateStates(correct,totalCorrect,totalWrong,earing);
break;
case '5':
displayStats(totalCorrect,totalWrong,earing, name1);
break;
case '6':
saveStats(totalCorrect,totalWrong,earing, name1);
break;
case 'n':
cout << " good bye" << endl;
return 'n';
case 'N':
cout << " good bye" << endl;
return 'N';
}
}while (ch=='n'|| ch =='N');
return 0;
}
// check answers
bool checkUserAnswer(double answer, double result)
{
if (answer == result)
{
cout << " RIGHT" << endl;
cout << endl;
return true;
}
else
{
cout << " WRONG" << endl;
cout << endl;
return false;
}
}
// update
void updateStates(bool correct, int *totalCorrect, int *totalWrong, double *earing)
{
if (correct)
{
*totalCorrect = *totalCorrect+1;
*earing = *earing + 0.05;
}
else
{
*totalWrong = *totalWrong +1;
*earing = *earing -0.03;
}
}
// display
void displayStats(int *totalCorrect, int *totalWrong, double *earing, string name)
{
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "** " << name << " **" << endl;
cout << " Total Earing: " << *earing << endl;
cout << " Total Correct: " << *totalCorrect << endl;
cout << " Total Wrong: " << *totalWrong << endl;
cout << "***********************" << endl;
}
// generateAddition
double generateAddition(double number1, double number2)
{
double answer;
cout << " ADDITION " << endl;
cout << number1 << " + " << number2 <<" = ? " << endl;
//getline(cin,answer);
cin >> answer;
while (!cin.good())
{
cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
// generateSubstraction
double generateSubstraction(double number1, double number2)
{
double answer;
cout << " SUBSTRACTION " << endl;
cout << number1 << " - " << number2 << " = ?" << endl;
cin >> answer;
while (!cin.good())
{
cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
//Multiplication
double generateMultiplication(double number1, double number2)
{
double answer;
cout << " MULTIPLICATION " << endl;
cout << number1 << " * " << number2 << " = ?" << endl;
cin >> answer;
while (!cin.good())
{
cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
// DIVISION
double generateDivision(double number1, double number2)
{
double answer;
cout << " DIVISION" << endl;
cout << number1 << " / " << number2 << " = ?" << endl;
cin >> answer;
while (!cin.good())
{
cin.clear();
cin.ignore(1000000, ' ');
cout << " That is not a number " << endl;
cin >> answer;
}
return answer;
}
// SAVE
void saveStats(int *totalCorrect, int *totalWrong, double *earing, string name)
{
ofstream myfile;
myfile.open ( "stats.txt");
myfile << "***************" << endl;
myfile << name << endl;
myfile << "Total Correct: " << *totalCorrect << endl;
myfile << "Total Wrong: " << *totalWrong << endl;
myfile << "Total earing: " << *earing << endl;
myfile << "***************" << endl;
myfile.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.