Help me to take out pointers \"*\" and change it to pass by reference only #incl
ID: 663734 • Letter: H
Question
Help me to take out pointers "*" and change it to pass by reference only
#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(int answer, int result);
char menu(int *totalCorrect, int *totalWrong, double *earing, string name);
int generateAddition(int number1, int number2);
int generateSubstraction(int number1, int number2);
int generateMultiplication(int number1, int number2);
int generateDivision(int number1, int number2);
void saveStats(int *totalCorrect, int *totalWrong, double *earing, string name);
void validate(string& str1, int& userNumber);
int main()
{
char ch;
string name;
int totalCorrect = 0 ;
int totalWrong = 0 ;
double earning = 0.0;
if(credits(name))
{
while(true)//while(credits(name))
{
ch = menu(&totalCorrect,&totalWrong,&earning, name);
if (ch == 'n' || ch == 'N')
break;
}
}
else
{
cout << " GOOD BYE !" << endl;
cout << endl;
}
return 0;
}
void validate(string& str1, int& userNumber)
{
char nextChar;
int strLength = str1.length();
int counter = 0;
//the user will be stuck in the following loop until he/she eneters a non-negative number
while( counter < strLength || strLength == 0)
{
if(!isdigit(str1[counter]))
{
cout << "that is not a number > 0, try again ";
getline(cin,str1);
strLength = str1.length();
counter = 0;
}
else
{
counter++;
}
}
//this next loop converts the users "string" input to numeric
//note that the program will execute this loop ONLY after we exit from the loop above
//which actually makes sure we get a number from the user
counter = 0;
while (counter < strLength)
{
nextChar = str1[counter];
//also note that we don't really need an if statement to check
// if nextChar is a digit
//this loop works just fine if we use
//userNumber = userNumber*10 + (nextChar - '0');
if(isdigit(nextChar))
{
userNumber = userNumber*10 + (nextChar - '0');
}
counter++;
}
}
// Math Game //
bool credits(string &name)
{
char ch;
cout << "*******************" << endl;
cout << "*******************" << endl;
cout << "*** Math Game *****" << endl;
cout << "*** *****" << endl;
cout << "*** *****" << endl;
cout << "*******************" << endl;
cout << "*******************" << endl;
cout << "y/Y to contiune, or any other char to exit" << endl;
string nameplace;
string chString;
getline(cin, chString);
while(chString != "y" && chString != "Y")
{
cout << "no good, enter y/Y only" << endl;
getline(cin, chString);
}
ch = chString[0];
//cin >> ch;
//cin.clear();
//cin.ignore(1000000, ' ');
if (ch == 'y' || ch == 'Y')
{
//char nextChar;
int counter = 0;
cout << " Enter your Name and press Enter:" << endl;
getline(cin,nameplace);
int strLength = nameplace.length();
//the user will be stuck in the following loop until he/she eneters a non-negative number
while( counter < strLength || strLength == 0)
{
if(!isalpha(nameplace[counter]))
{
cout << "that is not a proper name, try again ";
getline(cin,nameplace);
strLength = nameplace.length();
counter = 0;
}
else
{
counter++;
}
}
return true;
}
}
// Menu
char menu(int *totalCorrect, int *totalWrong, double *earing, string name1 )
{
char ch;
bool correct;
string userInput;
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;
getline(cin, userInput);
while(userInput != "1" && userInput != "2" &&userInput != "3" &&userInput != "4" &&userInput != "5" &&userInput != "6" && userInput != "n")
{
cout << "no good" << endl;
getline(cin, userInput);
}
int number1,number2;
do
{
number1 = rand()%10+1;
number2 = rand()%10+1;
}
while(number1<number2);
double result; int answer;
do
{
switch(userInput[0])
{
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(int answer, int 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
int generateAddition(int number1, int number2)
{
int answer;
string userNumber;
cout << " ADDITION " << endl;
cout << number1 << " + " << number2 <<" = ? " << endl;
getline(cin,userNumber);
validate(userNumber, answer);
return answer;
}
// generateSubstraction
int generateSubstraction(int number1, int number2)
{
string userNumber;
int answer;
cout << " SUBSTRACTION " << endl;
cout << number1 << " - " << number2 << " = ?" << endl;
//cin >> answer;
getline(cin,userNumber);
validate(userNumber, answer);
return answer;
}
//Multiplication
int generateMultiplication(int number1, int number2)
{
int answer;
string userNumber;
cout << " MULTIPLICATION " << endl;
cout << number1 << " * " << number2 << " = ?" << endl;
getline(cin,userNumber);
validate(userNumber, answer);
return answer;
}
// DIVISION
int generateDivision(int number1, int number2)
{
string userNumber;
int answer;
cout << " DIVISION" << endl;
cout << number1 << " / " << number2 << " = ?" << endl;
getline(cin,userNumber);
validate(userNumber, 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
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h> //for system 'cls'
#include<iomanip>
#include<fstream>
#include<string>
void credits(); //opening splash screen (clone Professor Flores)
void menu(); //menu options including exit option
bool validateUserResponse(string); //catching user input
void checkUserAnswer(int, int, int&, int&, double&); // given a math problem, this function is used to check if the answer the user entered is correct/ incorrect
void updateStats(int&, double&); // used to keep a running total of game statistics
void updateStatsIncorrect(int&, double&);
void displayStats(string, int&, int&, double&); // display statistics on screen
void retireveStats(ifstream&, string&, int&, int&, double&); // retrieve player statistics from external txt file
void saveStats(ofstream&, string, int, int, double); // save player statistics on external txt file
void generateAddition(int&, int&, double&);
void generateSubtraction(int&, int&, double&);
void generateMultiplication(int&, int&, double&);
void generateDivision(int&, int&, double&);
void validateInt(int&);
void floresValidate(string str1, int& userip);
int main(string&, int&)
{
bool flag = true;
ofstream file;
ifstream infile;
string name;
int correct = 0;
int incorrect = 0;
double earning = 0;
string choice;
credits();
cout << "y/Y To Continue, Any Other Char To Exit: " << endl;
char ch;
cin >> ch;
if (ch != 'y' && ch != 'Y')
{
exit(0);
}
else
{
system("cls");
cin.ignore();
do
{
cout << "Enter your name and press <ENTER>" << endl;
do
{
getline(cin, name);
if (name.empty())
cout << "This is not a valid Name" << endl;
} while (name.empty());
flag = validateUserResponse(name);
} while (flag == false);
retireveStats(infile, name, correct, incorrect, earning);
system("cls");
while (true)
{
menu();
getline(cin, choice);
while (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "5" && choice != "n" && choice != "N")
{
cout << "That Is Not A Valid Menu Choice ";
getline(cin, choice);
}
switch (choice[0])
{
case '1':
system("cls");
generateAddition(correct, incorrect, earning);
saveStats(file, name, correct, incorrect, earning);
break;
case '2':
system("cls");
generateSubtraction(correct, incorrect, earning);
saveStats(file, name, correct, incorrect, earning);
break;
case '3':
system("cls");
generateMultiplication(correct, incorrect, earning);
saveStats(file, name, correct, incorrect, earning);
break;
case '4':
system("cls");
generateDivision(correct, incorrect, earning);
saveStats(file, name, correct, incorrect, earning);
break;
case '5':
char ch;
system("cls");
displayStats(name, correct, incorrect, earning);
system("pause");
system("cls");
break;
case 'n':
case 'N':
exit(0);
break;
default:
cout << "That is not a vallid Menu Option";
cin.ignore();
break;
}
}
}
}
void credits()
{
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "****** ******" << endl;
cout << "******TheMathGame******" << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << "***********************" << endl << endl;
}
void menu()
{
cout << endl;
cout << "******CHOOSE A PROBLEM******" << endl;
cout << "****************************" << endl;
cout << "****************************" << endl;
cout << "****** ******" << endl;
cout << "****** 1. ADD ******" << endl;
cout << "****** 2. SUBTRACT ******" << endl;
cout << "****** 3. MULTIPLY ******" << endl;
cout << "****** 4. DIVIDE ******" << endl;
cout << "****** 5. STATS ******" << endl;
cout << "****** n/N to QUIT ******" << endl;
cout << "****** ******" << endl;
cout << "****************************" << endl;
cout << "****************************" << endl;
}
void generateAddition(int &correct, int &incorrect, double &earning)
{
srand(time(NULL));
string userEntry;
int userip = 0;
int result;
int num1 = rand() % 19 + 1;
int num2 = rand() % 19 + 1;
cout << "******ADDITION******" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
cout << "***** " << num1 << " + " << num2 << "=? *****" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
result = num1 + num2;
//cin.ignore();
getline(cin, userEntry);
floresValidate(userEntry, userip);
checkUserAnswer(result, userip, correct, incorrect, earning);
}
void generateSubtraction(int &correct, int &incorrect, double &earning)
{
srand(time(NULL));
int result, num1, num2;
int userip = 0;
string userEntry;
recalc: num1 = rand() % 20;
num2 = rand() % 20;
if (num1<num2) //checks to make sure no negative answers aloud.
{
goto recalc;
}
cout << "****SUBTRACTION*****" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
cout << "***** " << num1 << " - " << num2 << "=? *****" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
result = num1 - num2;
/*cin.ignore();*/
getline(cin, userEntry);
floresValidate(userEntry, userip);
checkUserAnswer(result, userip, correct, incorrect, earning);
}
void generateMultiplication(int &correct, int &incorrect, double &earning)
{
srand(time(NULL));
int result, userip = 0;
string userEntry;
int num1 = rand() % 20;
int num2 = rand() % 20;
cout << "**MULTIPLICATION****" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
cout << "***** " << num1 << " * " << num2 << "=? *****" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
result = num1*num2;
/*cin.ignore();*/
getline(cin, userEntry);
floresValidate(userEntry, userip);
checkUserAnswer(result, userip, correct, incorrect, earning);
}
void generateDivision(int &correct, int &incorrect, double &earning)
{
srand(time(NULL));
int result, userip = 0, num1, num2;
string userEntry;
recalc: num1 = rand() % 20;
num2 = rand() % 10 + 1;
if (num1<num2 || !(num1 % num2 == 0)) //makes sure first number larger than second || first num IS divisible by second
{
goto recalc;
}
cout << "******DIVISION******" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
cout << "***** " << num1 << " / " << num2 << "=? *****" << endl;
cout << "********************" << endl;
cout << "********************" << endl;
result = num1 / num2;
/*cin.ignore();*/
getline(cin, userEntry);
floresValidate(userEntry, userip);
checkUserAnswer(result, userip, correct, incorrect, earning);
}
void checkUserAnswer(int correctans, int userans, int &correct, int &incorrect, double &earning)
{
system("cls");
if (userans == correctans)
{
cout << "***********RIGHT!***********" << endl;
updateStats(correct, earning);
}
else
{
cout << "***********WRONG!***********" << endl;
updateStatsIncorrect(incorrect, earning);
}
}
void updateStats(int &correct, double &earning)
{
correct++;
earning += 0.05;
}
void updateStatsIncorrect(int &incorrect, double &earning)
{
incorrect++;
earning -= 0.03;
}
void displayStats(string name, int &correct, int &incorrect, double &earning)
{
cout << "********************************************" << endl;
cout << "********************************************" << endl;
cout << "********************************************" << endl;
cout << "****** ******" << endl;
cout << "******" << setw(32) << left << name << "******" << endl;
cout << "******" << setw(17) << left << "Total Earnings $" << setw(15) << left << earning << "******" << endl;
cout << "******" << setw(17) << left << "Total Correct " << setw(15) << left << correct << "******" << endl;
cout << "******" << setw(17) << left << "Total Incorrect " << setw(15) << left << incorrect << "******" << endl;
cout << "****** ******" << endl;
cout << "********************************************" << endl;
cout << "********************************************" << endl;
cout << "********************************************" << endl;
}
void saveStats(ofstream &file, string name, int correct, int incorrect, double earning)
{
string str;
str = name + ".txt";
file.open(str.c_str());
file << name << endl;
file << earning << endl;
file << correct << endl;
file << incorrect << endl;
file.close();
}
void retireveStats(ifstream &infile, string &name, int &correct, int &incorrect, double &earning)
{
string fileName, fileVal, str;
str = name + ".txt";
infile.open(str.c_str());
if (infile)
{
infile >> fileName;
name = fileName;
infile >> fileVal;
earning = atof(fileVal.c_str());
infile >> fileVal;
correct = atoi(fileVal.c_str());
infile >> fileVal;
incorrect = atoi(fileVal.c_str());
}
infile.close();
}
bool validateUserResponse(string name)
{
bool secflag = true;
int i = 0;
while (name[i] != NULL)
{
if (!isalpha(name[i]))
{
cout << "Not a valid name" << endl;
secflag = false;
break;
}
++i;
}
return secflag;
}
void validateInt(int &userip)
{
while (!cin)
{
cout << "That was not an integer! Please Enter a number " << endl;
cin.clear();
cin.ignore();
cin >> userip;
}
}
void floresValidate(string str1, int& userip)
{
char nextChar;
int strLength = str1.length();
int counter = 0;
userip = 0;
while (counter < strLength || strLength == 0)
{
if (!isdigit(str1[counter]))
{
cout << "that is not a number > 0, try again ";
getline(cin, str1);
strLength = str1.length();
counter = 0;
}
else
{
counter++;
}
}
counter = 0;
while (counter < strLength)
{
nextChar = str1[counter];
if (isdigit(nextChar))
{
userip = userip * 10 + (nextChar - '0');
}
counter++;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.