hey guys, so for this math game i manage to do allot but there is this 2 parts i am totally lost so if anyone can help that will be perfect
first here is the code
now the first problem is that when the user start the game play few times he can click N or n which will close the game and save his stats. now lets say i have my text file with my name in the folder and i want to play with my old record i should be able to put the same name which will load the stats back in the program but i dont know how to do that please help me there.
the next problem i dont get is, every time you get correct answer it gives 5 cent every wrong will take 3 cent this works perfect if i am only playing add game but if lets say i play 1 add game and one subtract game the scores are getting buged.
first here is the code
Code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
void addition();
void subtraction();
void multiplication();
void division();
void stats();
void quit();
void checkAnswer(int userAnswer, int realAnswer);
string userName ("");
float money (0.00f);
int correct (0), incorrect (0);
ifstream LoadFile;
ofstream outFile;
int main()
{
cout << "************************\n" <<
"************************\n" <<
"************************\n" <<
"****** ******\n" <<
"******TheMathGame ******\n" <<
"****** BY ******\n" <<
"****** Boris ******\n" <<
"****** Grigoryan ******\n" <<
"****** ******\n" <<
"************************\n" <<
"************************\n" <<
"************************\n";
cout << "\nType \"Y\" or \"y\" in order to play, any other inputs will exit.\n";
string inputString;
getline(cin, inputString);
if(inputString == "y" || inputString == "Y")
{
system("cls");
cout << "Whats is your name good sir?" << endl;
getline(cin, userName);
if (userName == userName+".txt")
{
// if the name is the same as the text then it should load back the stats
// how many correct he got his money and the incorrect.
}
else
{
outFile.open (userName+".txt");
}
while(true)
{
system("cls");
cout << "************PICK ONE**********\n" <<
"******************************\n" <<
"******************************\n" <<
"****** ******\n" <<
"****** 1. ADD ******\n" <<
"****** 2. SUBTRACT ******\n" <<
"****** 3. MULTIPLY ******\n" <<
"****** 4. DIVIDE ******\n" <<
"****** 5. STATS ******\n" <<
"****** n/N to QUIT ******\n" <<
"****** ******\n" <<
"******************************\n" <<
"******************************\n" <<
"******************************\n";
char inputChar (0);
getline(cin, inputString);
if (inputString.length() == 1)
{
inputChar = inputString[0];
}
switch (inputChar)
{
case '1':
addition();
break;
case '2':
subtraction();
break;
case '3':
multiplication();
break;
case '4':
division();
break;
case '5':
stats();
break;
case 'n':
case 'N':
quit();
return 0;
break;
default:
break;
}
}
}
else
{
return 0;
}
return 0;
}
void addition()
{
unsigned int X (rand() % 10 + 1), Y (rand() % 10 + 1);
unsigned int Z (X + Y);
system("cls");
cout << "************ADDITION**********\n" <<
"******************************\n" <<
"****** "<< X <<" + "<< Y <<" = ******\n" <<
"******************************\n" <<
"******************************\n";
int answer;
cin >> answer;
checkAnswer(answer, Z);
}
void subtraction()
{
int X (rand() % 20 + 11), Y (rand() % 10 + 1);
int Z (X - Y);
system("cls");
cout << "**********SUBTRACTION*********\n" <<
"******************************\n" <<
"****** "<< X <<" - "<< Y <<" = ******\n" <<
"******************************\n" <<
"******************************\n";
int answer;
cin >> answer;
checkAnswer(answer, Z);
}
void multiplication()
{
int X (rand() % 10 + 1), Y (rand() % 10 + 1);
int Z (X * Y);
system("cls");
cout << "*********MULTIPLICATION*******\n" <<
"******************************\n" <<
"****** "<< X <<" * "<< Y <<" = ******\n" <<
"******************************\n" <<
"******************************\n";
int answer;
cin >> answer;
checkAnswer(answer, Z);
}
void division()
{
int X (rand() % 20 + 11), Y (rand() % 10 + 1);
int Z (X / Y);
system("cls");
cout << "************DIVISION**********\n" <<
"******************************\n" <<
"****** "<< X <<" / "<< Y <<" = ******\n" <<
"******************************\n" <<
"******************************\n";
int answer;
cin >> answer;
checkAnswer(answer, Z);
}
void stats()
{
system("cls");
cout << userName << "'s Stats: " << endl <<
"Money: " << money << endl <<
"Correct: " << correct << endl <<
"Incorrect: " << incorrect << endl << endl <<
"Types Something To Continue" << endl;
cin.get();
}
void checkAnswer(int userAnswer, int realAnswer)
{
system("cls");
if (userAnswer == realAnswer)
{
money += 0.05f;
correct++;
cout << "************CORRECT***********\n" <<
"******************************\n" <<
"****** Answer: "<< realAnswer <<" ******\n" <<
"******************************\n" <<
"******************************\n";
}
else
{
money -= 0.03f;
incorrect++;
cout << "**************WRONG***********\n" <<
"******************************\n" <<
"****** Answer: "<< realAnswer <<" ******\n" <<
"******************************\n" <<
"******************************\n";
}
cin.get();
cin.get();
}
void quit()
{
outFile << userName << "'s Stats: " << endl <<
outFile << "Money: " << money << endl <<
outFile << "Correct: " << correct << endl <<
outFile << "Incorrect: " << incorrect << endl << endl;
outFile.close();
}
now the first problem is that when the user start the game play few times he can click N or n which will close the game and save his stats. now lets say i have my text file with my name in the folder and i want to play with my old record i should be able to put the same name which will load the stats back in the program but i dont know how to do that please help me there.
the next problem i dont get is, every time you get correct answer it gives 5 cent every wrong will take 3 cent this works perfect if i am only playing add game but if lets say i play 1 add game and one subtract game the scores are getting buged.