Hi I am currently in school right now as a senior in college with a major of Electrical Enginnering getting ready to graduate in Dec. I know some programming but it comes more on the hardware side like microcontrollers interatcing with sensors and things like that with GPIO pins etc.. so nothing too crazy. But I have always liked games and like coding classes I have taken. Dont get me wrong I love my major but I also like the progamming aspect. The only class I have taken was a class in C and then another class of continuation into C++. So I have started small and made a basic text based guess the number game with different functions such as saving scores to a file with a name, an exiting function, display all the scores and delete all the score. What I am looking for is some advice on structure and good coding pratices thing that I should do better on and some basic advice. Any advice would be greatly appericated.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int play(); // play the game
void save(int score); // save score in a file
int menu(); // display a menu
void deleteData(); // delete the high scores
void displayScores(); // display the scores
int main()
{
int status; // returned from function
int score = 0; // how many times you have won
int menuState = -1; // menu state
bool game; // game state of game
cout << "Get a score of 5 and win but three wrong guesses in a row and you lose\n";
while (menuState != 2)
{
menuState = menu();
game = true;
if(menuState == 1)
{
while(game == true)
{
status = play();
if (status == 1)
{
score += status;
cout << "Score: " << score << "\n\n";
}
if (status == -1)
{
cout << "Sorry you are a loser!!!\n";
cout << "Your final score was: " << score << " points\n";
save(score);
score = 0;
menuState = -1;
game = false;
}
if (score == 5)
{
cout << "Congrats you have won the Game!!\n";
cout << "Your final score was: " << score << " points\n";
save(score);
score = 0;
menuState = -1;
game = false;
}
}
}
if(menuState == 3)
{
displayScores();
}
if(menuState == 4)
{
deleteData();
}
}
cout << "Exiting Game....\n";
system("PAUSE");
return 0;
}
int play()
{
int randomNumber = rand() % 100 + 1;
int tries = 0;
int guess;
while (tries < 3)
{
cout << "Guess a number between 1 and 100: ";
cin >> guess;
if (guess == randomNumber)
{
cout << "Congrats you guessed correctly\n";
return 1;
}
else if (guess < randomNumber)
{
cout << "You guessed too low try again\n";
tries++;
}
else if (guess > randomNumber)
{
cout << "You guessed too high try again\n";
tries++;
}
}
cout << "The number was " << randomNumber << "\n";
return -1;
}
void save(int score)
{
string name;
cout << "Please enter you name: ";
cin >> name;
ofstream file;
file.open("scores.txt",ios::app);
if(file.is_open())
{
file << name << " " << score << "\n";
}
else
{
cout << "Unable to open file!!\n";
}
file.close();
}
int menu()
{
int selection = -1;
while(selection < 1 || selection > 3)
{
cout << "Make a selection\n";
cout << "1 - Play Game\n";
cout << "2 - Exit Game\n";
cout << "3 - Display High Scores\n";
cout << "4 - Erase High Scores\n";
cout << "Input: ";
cin >> selection;
if(selection == 1)
return 1;
else if(selection == 2)
return 2;
else if(selection == 3)
return 3;
else if(selection == 4)
return 4;
else
cout << "Please enter a valid selection!\n";
}
}
void deleteData()
{
ofstream file;
file.open("scores.txt",ios::out | ios::trunc);
file.close();
}
void displayScores()
{
string name;
int score;
ifstream file;
file.open("scores.txt");
if(file.is_open())
{
while(true)
{
if(!(file >> name)) break;
cout << name << "\t";
if(!(file >> score)) break;
cout << score << "\n";
}
}
else
{
cout << "File could not be opened..\n";
}
file.close();
}