I have this running C++ program.However, i must convert it to mostly classes and
ID: 662551 • Letter: I
Question
I have this running C++ program.However, i must convert it to mostly classes and having mostly calls to instace variables in main. i need some assistance with that. here is my running program:
!!!!!!!!!! CHANGE ALL TO their respective CLASSES WITH MOSTLY CALLS TO INSTANCE VARIAbles in MAIN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
#include "stdafx.h"
#include < iomanip >
#include < iostream >
#include < cmath >
#include < string >
#include < cstdlib >
#include < fstream >
#include "additionWindow.h"
using namespace std;
void splash_screen();
void menu();
//void additionWindow(int& total_correct, int& total_wrong, double& total_bonus);
void subtractionWindow(int& total_correct, int& total_wrong, double& total_bonus);
void multiplicationWindow(int& total_correct, int& total_wrong, double& total_bonus);
void divisionWindow(int& total_correct, int& total_wrong, double& total_bonus);
char validateChar(char& problemSelection);
void validateInt(int& userAnswer);
void request_name(string& user_name);
int checkUserAnswer(int correctAnswer, int userAnswer, int& total_correct, int& total_wrong, double& total_bonus);
void display_stats(int& total_correct, int& total_wrong, double& total_bonus, string user_name);
void retrieveStats(string& user_name, int& total_correct, int& total_wrong, double& total_bonus, ifstream& inData);
void save_stats(string& user_name, int& total_correct, int& total_wrong, double& total_bonus, ofstream& outData);
int main()
{
splash_screen();
ifstream inData; //Declare file stream variables
ofstream outData;
int total_correct = 0;
int total_wrong = 0;
double total_bonus = 0;
bool Response = false;//Used to check if user's response was correct
char continueOrExitKey = 0;
char dummy;
char problemSelection = 0;
cout << endl << endl;
cout << "Note: Every CORRECT answer = +$0.05" << endl;
cout << " Every INCORRECT answer = -$0.03" << endl;
cout << "y/Y to continue, any other char to exit" << endl;
cin >> continueOrExitKey;
cin.get(dummy);//Enter Key
system("CLS");
if (continueOrExitKey == 'y' || continueOrExitKey == 'Y')
{
cout << "Enter your name and then press <ENTER>" << endl;
string user_name;
getline(cin, user_name);
request_name(user_name);
retrieveStats(user_name, total_correct, total_wrong, total_bonus, inData);
system("CLS");
menu();
cin >> problemSelection;
cin.get(dummy);
while (problemSelection != 'n' || problemSelection != 'N')
{
system("CLS");
validateChar(problemSelection);
switch (problemSelection)
{
case '1':
additionWindow(total_correct, total_wrong, total_bonus);
break;
case '2':
subtractionWindow(total_correct, total_wrong, total_bonus);
break;
case '3':
multiplicationWindow(total_correct, total_wrong, total_bonus);
break;
case '4':
divisionWindow(total_correct, total_wrong, total_bonus);
break;
case '5':
display_stats(total_correct, total_wrong, total_bonus, user_name);
break;
case 'n':
save_stats(user_name, total_correct, total_wrong, total_bonus, outData);
return 0;
case 'N':
save_stats(user_name, total_correct, total_wrong, total_bonus, outData);
return 0;
default:
cout << "Enter 1-5, n, or N" << endl; //How to prevent user from entering many values
}
menu();
cin >> problemSelection;
system("CLS");
}
}
system("pause");
return 0;
}
void splash_screen()
{
cout << "******\*********|********/*******" << endl;
cout << "*******\********|*******/********" << endl;
cout << "********\*******|******/*********" << endl;
cout << "*#*#*#*# #*#*#*#*" << endl;
cout << "#*#*#*#* The Math Game *#*#*#*#" << endl;
cout << "-------- --------" << endl;
cout << "#*#*#*#* *#*#*#*#" << endl;
cout << "*#*#*#*# #*#*#*#*" << endl;
cout << "*******/********|********\*******" << endl;
cout << "******/*********|*********\******" << endl;
cout << "*****/**********|**********\*****" << endl;
}
void menu()
{
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 additionWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = rand() % 100;
addNum2 = rand() % 100 + 1;
cout << "*********\\\Addition///**********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 << " + " << addNum2 << " = " << "??? " << setw(7) << "*********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = addNum1 + addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}*/
void subtractionWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = (rand() % 100) + 101;
addNum2 = rand() % 100;
cout << "*******\\\Subtraction///*********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 << " - " << addNum2 << " = " << " ? " << "***********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = addNum1 - addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}
void multiplicationWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = rand() % 13;
addNum2 = rand() % 13;
cout << "*****\\\Multiplication///********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 << " * " << addNum2 << " = " << "??? " << setw(7) << "**********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = addNum1 * addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}
void divisionWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = (rand() % 10);
addNum2 = rand() % 10 + 1;
cout << "********\\\Division///***********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 * addNum2 << " / " << addNum2 << " = " << "???" << setw(13) << "********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = (addNum1 * addNum2) / addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}
char validateChar(char& problemSelection)
{
while (!cin.good() || problemSelection <= 0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
cout << "try again, a number between 1 and 4 please" << endl;
cin >> problemSelection;
}
return problemSelection;
}
void request_name(string& user_name)
{
char nextChar;
int strLength = user_name.length();
int counter = 0;
while (counter < strLength || strLength == 0)
{
if (!isalpha(user_name[counter]))
{
cout << "that is not a name, please enter a name" << endl;
getline(cin, user_name);
strLength = user_name.length();
counter = 0;
}
else
{
counter++;
}
}
}
void validateInt(int& userAnswer)
{
while (!cin.good() || userAnswer < 0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
cout << "This is not a positive interger" << endl;
cout << "Enter a number!" << endl;
cin >> userAnswer;
}
}
int checkUserAnswer(int correctAnswer, int userAnswer, int& total_correct, int& total_wrong, double& total_bonus)
{
system("CLS");
if (correctAnswer == userAnswer)
{
total_correct += 1;
total_bonus += .05;
cout << "Correct" << endl;
}
if (correctAnswer != userAnswer)
{
total_wrong += 1;
total_bonus -= .03;
cout << "Incorrect" << endl;
}
return 0;
}
void display_stats(int& total_correct, int& total_wrong, double& total_bonus, string user_name)
{
system("CLS");
cout << user_name << endl;
cout << "Total Correct :" << total_correct << endl;
cout << "Total incorrect is :" << total_wrong << endl;
cout << "Total bonus is :$" << total_bonus << endl;
char statChar;
cout << "Press any key to continue....." << endl;
cin >> statChar;
cin.clear();
system("CLS");
}
void retrieveStats(string& user_name, int& total_correct, int& total_wrong, double& total_bonus, ifstream& inData)
{
string str1 = user_name;
string str2 = ".txt";
string fileName = str1 + str2;
inData.open(fileName);
inData >> user_name;
inData >> total_correct;
inData >> total_wrong;
inData >> total_bonus;
inData.close();
}
void save_stats(string& user_name, int& total_correct, int& total_wrong, double& total_bonus, ofstream& outData)
{
string str1 = user_name;
string str2 = ".txt";
string fileName = str1 + str2;
outData.open(fileName);
outData << user_name << endl;
outData << total_correct << endl;
outData << total_wrong << endl;
outData << total_bonus << endl;
outData.close();
}
Explanation / Answer
#include "stdafx.h"
#include < iomanip >
#include < iostream >
#include < cmath >
#include < string >
#include < cstdlib >
#include < fstream >
#include "additionWindow.h"
using namespace std;
class convert
{
void splash_screen()
{
cout << "******\*********|********/*******" << endl;
cout << "*******\********|*******/********" << endl;
cout << "********\*******|******/*********" << endl;
cout << "*#*#*#*# #*#*#*#*" << endl;
cout << "#*#*#*#* The Math Game *#*#*#*#" << endl;
cout << "-------- --------" << endl;
cout << "#*#*#*#* *#*#*#*#" << endl;
cout << "*#*#*#*# #*#*#*#*" << endl;
cout << "*******/********|********\*******" << endl;
cout << "******/*********|*********\******" << endl;
cout << "*****/**********|**********\*****" << endl;
}
void menu()
{
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 additionWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = rand() % 100;
addNum2 = rand() % 100 + 1;
cout << "*********\\\Addition///**********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 << " + " << addNum2 << " = " << "??? " << setw(7) << "*********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = addNum1 + addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}*/
void subtractionWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = (rand() % 100) + 101;
addNum2 = rand() % 100;
cout << "*******\\\Subtraction///*********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 << " - " << addNum2 << " = " << " ? " << "***********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = addNum1 - addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}
void multiplicationWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = rand() % 13;
addNum2 = rand() % 13;
cout << "*****\\\Multiplication///********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 << " * " << addNum2 << " = " << "??? " << setw(7) << "**********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = addNum1 * addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}
void divisionWindow(int& total_correct, int& total_wrong, double& total_bonus)
{
int addNum1 = 0;
int addNum2 = 0;
srand(time(NULL));
addNum1 = (rand() % 10);
addNum2 = rand() % 10 + 1;
cout << "********\\\Division///***********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
cout << "********" << addNum1 * addNum2 << " / " << addNum2 << " = " << "???" << setw(13) << "********" << endl;
cout << "*********************************" << endl;
cout << "*********************************" << endl;
int correctAnswer = (addNum1 * addNum2) / addNum2;
int userAnswer = 0;
cin >> userAnswer;
validateInt(userAnswer);
checkUserAnswer(correctAnswer, userAnswer, total_correct, total_wrong, total_bonus);
}
char validateChar(char& problemSelection)
{
while (!cin.good() || problemSelection <= 0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
cout << "try again, a number between 1 and 4 please" << endl;
cin >> problemSelection;
}
return problemSelection;
}
void request_name(string& user_name)
{
char nextChar;
int strLength = user_name.length();
int counter = 0;
while (counter < strLength || strLength == 0)
{
if (!isalpha(user_name[counter]))
{
cout << "that is not a name, please enter a name" << endl;
getline(cin, user_name);
strLength = user_name.length();
counter = 0;
}
else
{
counter++;
}
}
}
void validateInt(int& userAnswer)
{
while (!cin.good() || userAnswer < 0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
cout << "This is not a positive interger" << endl;
cout << "Enter a number!" << endl;
cin >> userAnswer;
}
}
int checkUserAnswer(int correctAnswer, int userAnswer, int& total_correct, int& total_wrong, double& total_bonus)
{
system("CLS");
if (correctAnswer == userAnswer)
{
total_correct += 1;
total_bonus += .05;
cout << "Correct" << endl;
}
if (correctAnswer != userAnswer)
{
total_wrong += 1;
total_bonus -= .03;
cout << "Incorrect" << endl;
}
return 0;
}
void display_stats(int& total_correct, int& total_wrong, double& total_bonus, string user_name)
{
system("CLS");
cout << user_name << endl;
cout << "Total Correct :" << total_correct << endl;
cout << "Total incorrect is :" << total_wrong << endl;
cout << "Total bonus is :$" << total_bonus << endl;
char statChar;
cout << "Press any key to continue....." << endl;
cin >> statChar;
cin.clear();
system("CLS");
}
void retrieveStats(string& user_name, int& total_correct, int& total_wrong, double& total_bonus, ifstream& inData)
{
string str1 = user_name;
string str2 = ".txt";
string fileName = str1 + str2;
inData.open(fileName);
inData >> user_name;
inData >> total_correct;
inData >> total_wrong;
inData >> total_bonus;
inData.close();
}
void save_stats(string& user_name, int& total_correct, int& total_wrong, double& total_bonus, ofstream& outData)
{
string str1 = user_name;
string str2 = ".txt";
string fileName = str1 + str2;
outData.open(fileName);
outData << user_name << endl;
outData << total_correct << endl;
outData << total_wrong << endl;
outData << total_bonus << endl;
outData.close();
}
};
int main()
{
convert obj;
obj.splash_screen();
ifstream inData; //Declare file stream variables
ofstream outData;
int total_correct = 0;
int total_wrong = 0;
double total_bonus = 0;
bool Response = false;//Used to check if user's response was correct
char continueOrExitKey = 0;
char dummy;
char problemSelection = 0;
cout << endl << endl;
cout << "Note: Every CORRECT answer = +$0.05" << endl;
cout << " Every INCORRECT answer = -$0.03" << endl;
cout << "y/Y to continue, any other char to exit" << endl;
cin >> continueOrExitKey;
cin.get(dummy);//Enter Key
system("CLS");
if (continueOrExitKey == 'y' || continueOrExitKey == 'Y')
{
cout << "Enter your name and then press <ENTER>" << endl;
string user_name;
getline(cin, user_name);
obj.request_name(user_name);
obj.retrieveStats(user_name, total_correct, total_wrong, total_bonus, inData);
system("CLS");
obj.menu();
cin >> problemSelection;
cin.get(dummy);
while (problemSelection != 'n' || problemSelection != 'N')
{
system("CLS");
obj.validateChar(problemSelection);
switch (problemSelection)
{
case '1':
additionWindow(total_correct, total_wrong, total_bonus);
break;
case '2':
obj.subtractionWindow(total_correct, total_wrong, total_bonus);
break;
case '3':
obj.multiplicationWindow(total_correct, total_wrong, total_bonus);
break;
case '4':
obj.divisionWindow(total_correct, total_wrong, total_bonus);
break;
case '5':
obj.display_stats(total_correct, total_wrong, total_bonus, user_name);
break;
case 'n':
obj.save_stats(user_name, total_correct, total_wrong, total_bonus, outData);
return 0;
case 'N':
obj.save_stats(user_name, total_correct, total_wrong, total_bonus, outData);
return 0;
default:
cout << "Enter 1-5, n, or N" << endl; //How to prevent user from entering many values
}
obj.menu();
cin >> problemSelection;
system("CLS");
}
}
system("pause");
return 0;
}