Help With Hangman Game!!!

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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace 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;
						}
					}
			
			}
}
0x3A28213A
0x6339392C
0x7363682E

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).

Topic archived. No new replies allowed.