Hi everyone,
I've just about got this game running but am having some trouble exiting the game when the player is asked if they wish to continue. Can someone please look at my code and give me any pointers...
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
string get_Secret_Word(ifstream& infile);
void PlayGame(string word, ifstream& infile);
int main()
{
ifstream infile;
infile.open("mp4wordguess.txt");
while (infile)
{ //cant figure out how to break out of this loop with input provided from PlayGame function!!!
if (!infile)
{
cout << "BAD FILE!!!" << endl;
}
else
{
PlayGame(get_Secret_Word(infile), infile);
}
}
infile.close();
system("pause");
return 0;
}
string get_Secret_Word(ifstream& infile) //Reads secret word from infile
{
string word;
infile >> word;
return word;
}
void PlayGame(string word, ifstream& infile) //Plays the game
{
int wrong = 0;
char guess, incorrect, guessing;
string letters, word_guessed;
enum game{playing, won, lossed};
game game_status;
cout << "Word guessing game by: Kyle Slusher!!! \n";
cout << "\n";
cout << "Starting New Game!\n";
cout << "\n";
size_t pos1;
size_t length = word.length();
for (pos1=0; pos1 < length; pos1++) // Masks each letter of the secret word with a '*'
{
letters+= '*';
}
cout << "The Secret Word has " << length << " letters to guess" << endl; //Tells how many letters are in the secret word read from file
while(infile)
{
cout << "\nPlease Enter a Capitol Letter!" << endl; //Prompts user to guess letter
cin >> guess;
int count = 0;
for (pos1=0; pos1 <length; pos1++) //Compares users guess to the word
{
if (guess == word[pos1])
{
cout << "Good Guess!" << endl;
letters[pos1] = guess;
count++;
}
}
if (count==0) //You lose when you get 7 wrong letters
{
incorrect = guess;
cout << "Sorry, Wrong Letter!" << incorrect << endl;
wrong++;
if (wrong==7)
{
cout << "Sorry, GameOver! You got seven guesses wrong!" << endl;
cout << "Your word was " << word << endl;
cout << endl;
game_status = lossed;
}
}
cout << "The Word Is Now:" << letters << endl;
cout << "Do You Want To Guess The Word? 'Y' or 'N'" << endl; //Asks user if they would like to guess whole word
cin >> guessing;
if (guessing == 'Y')
{
cout << "Enter your guess:" << endl; //Prompts user to guess whole word
cin >> word_guessed;
if (word_guessed == word) //Compares users guessed_word to secret word
{
cout << "That is CORRECT!!!" << endl;
cout << "The word is: " << word << " You Won!" << endl;
game_status = won;
}
else
{
cout << "Sorry, " << word_guessed << " is incorrect!" << endl;
game_status = lossed;
}
}
if (letters == word) //Compares guessed letters to secret word
{
cout << "You Won!!!" << endl;
game_status = won;
}
if (game_status == won || game_status == lossed)
{
cout << "Would you like to play again? 'Y' or 'N'" << endl;
char play;
cin >> play;
if (play == 'N')
{
break;
}
}
}
}
Being serious, your code which asks the user if they want to play again does the same thing for both conditions. Also the if control is redundant - the game will always be won or lost, so it doesn't really need to be in there.
I couldn't get this to work properly on my machine, but that might be to do with the textfile (no idea what to put in there).