Help with Hangman Game

Newbie here, need help with my code for Hangman.
Here is my code

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
147
148
149
150
151
// Hangman.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <conio.h>
#include <time.h>

using namespace std;

//
//Hangman Game

string LoadFile() //To load the file
{
	
	string W; //Declare Varibles
	string words[20];
	int count = 0;
	
	ifstream txtfile; //the in file
	
	txtfile.open("words.txt"); //opens file

	for (count=0; !txtfile.eof(); count++)
	{
		getline(txtfile,W);
		words[count] = W;
	}
	int k=(rand()%count); //Picks a random word from file.
	txtfile.close(); //closes file
	
	return words[k];
}	


void RunGame(string word) //Runs the game
{
	int k;
	int wrong = 0;
	char guess;	
	string letters;
	char x;
	string incorrect;
	string correct;

	

	for (k=0; k<word.length();k++) //Blanks for each letter of your word
	{
		letters+= '_';
	}

	cout << word << endl;
	cout << endl;

	for (x=0; x<15; x++) //15 Guesses
	{
			cout << "Make a Guess" << endl;
			cin >> guess;

			int count = 0;

			for (k=0; k<word.length();k++) //Compare you guess to the word
			{
				if (guess == word[k])
				{
					cout << "Good Guess!" << endl;
					letters[k] = guess;
					correct += guess;
					count++;	
				}
				
				else 
				{
					incorrect += guess;
					
				}

			}


	
			if (count==0) //You lose when you get 6 wrong letters
			{
				cout << "Wrong Letter!" << endl;
				
				wrong++;
					if (wrong==6)
					{
						cout << "You are Dead! GameOver!" << endl;
						cout << "Your word was " << word << endl;
						cout << endl;
						break;
					}
			}
			

			cout << "Word:" << letters << endl;
			cout << "Correct:" << correct << endl;
			cout << "Incorrect:" << incorrect << endl;
			cout << endl;

			
	}

		

}



int _tmain(int argc, _TCHAR* argv[]) //main
{
	cout << "Let's Play HangMan!" << endl;
	
	srand(time(NULL));

	char playagain = 'Y';



	while (playagain == 'Y')
	{
		string word = LoadFile();
		RunGame(word);
		cout << endl;
		cout << "Play Again? (Y/N)" << endl;
		cin >> playagain;
		playagain = toupper(playagain);
	}

	




	cout << "Thank You for Playing" << endl;
	
	
	_getch();
	return 0;
}





The game doesn't seem to work right. I don't know what I'm doing wrong. Please help with fixing my code.
Last edited on
bump

Please help, I don't understand what I am doing wrong...
well tell us what the problem is. compiler errors? runtime errors? crashing?

also i dont really see any need for _tmain here. generally its best to just stick with main unless you want unicode characters.
Last edited on
I know this is off topic, but if you #include <windows.h> you can use Sleep(200); where the number inside is the time in milliseconds. That way you can use a code like this to make your game seem better

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cout << "Loading";
Sleep(200);
system("cls");
cout << "Loading.";
Sleep(200);
system("cls");
cout << "Loading..";
Sleep(200);
system("cls");
cout << "Loading...";
Sleep(200);
system("cls");
cout << "Loading";
Sleep(200);
system("cls");
cout << "Loading.";
Sleep(200);
system("cls");
cout << "Loading..";
Sleep(200);
system("cls");
cout << "Loading...";
Sleep(200);
system("cls");


Sorry, I know it's off topic, but I thought it's something you might have wanted to add to make it seem more real.
I see where you are going wrong. You are overpopulating your correct and incorrect strings. That is because you are adding the incorrect letter for every letter in the correct word during the for loop. Take that out and place is outside the for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

bool found = false;
for (k=0; k<word.length();k++) //Compare you guess to the word
{
	if (guess == word[k])
	{
		cout << "Good Guess!" << endl;
    	        letters[k] = guess;
		found = true;
 	        count++;	
	}
}
if(found)
    correct += guess;
else
   incorrect += guess;


The other problem is that the player can't win until the loop finishes i.e. the 15 guesses. Here is what you do. Don't reset count instead have a boolean called found which you set to false at the beginning of the for loop. If the player finds a correct letter, it is set to true skipping the wrong letter section. Keep incrementing count for each correct letter. If count is equal to word length then the player has won the game. Something like this:
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
    int count = 0;
	for (x=0; x<15; x++) //15 Guesses
	{
			cout << "Make a Guess" << endl;
			cin >> guess;

			

			bool found = false;
            for (k=0; k<word.length();k++) //Compare you guess to the word
            {
	            if (guess == word[k])
	            {
		            cout << "Good Guess!" << endl;
    	                    letters[k] = guess;
		            found = true;
 	                    count++;	
	            }
            }
            if(found)
                correct += guess;
            else
               incorrect += guess;

            if(count == word.length())
            {
                cout << "YOU HAVE WON!" << endl;
                break;
            }

	
			if (!found) //You lose when you get 6 wrong letters
			{
				cout << "Wrong Letter!" << endl;
				
				wrong++;
					if (wrong==6)
					{
						cout << "You are Dead! GameOver!" << endl;
						cout << "Your word was " << word << endl;
						cout << endl;
						break;
					}
			}


Last edited on
Thank you GodPyro, this really helped.

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
147
148
149
150
151
152
153
154

// Hangman.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cmath>
#include <conio.h>
#include <time.h>

using namespace std;

//Ramon Herrera
//Hangman Game

string LoadFile() //To load the file
{
	
	string W; //Declare Varibles
	string words[20];
	int count = 0;
	
	ifstream txtfile; //the in file
	
	txtfile.open("words.txt"); //opens file

	for (count=0; !txtfile.eof(); count++)
	{
		getline(txtfile,W);
		words[count] = W;
	}
	int k=(rand()%count); //Picks a random word from file.
	txtfile.close(); //closes file
	
	return words[k];
}	


void RunGame(string word) //Runs the game
{
	int k;
	int wrong = 0;
	char guess;	
	string letters;
	char x;
	string incorrect;
	string correct;

	

	for (k=0; k<word.length();k++) //Blanks for each letter of your word
	{
		letters+= '_';
	}



    int count = 0;
	for (x=0; x<15; x++) //15 Guesses
	{
			cout << "Make a Guess" << endl;
			cin >> guess;

			

			bool found = false;
            for (k=0; k<word.length();k++) //Compare you guess to the word
            {
	            if (guess == word[k])
	            {
		            cout << "Good Guess!" << endl;
    	                    letters[k] = guess;
		            found = true;
 	                    count++;	
	            }
            }
            if(found)
                correct += guess;
            else
               incorrect += guess;

            if(count == word.length())
            {
                cout << "YOU HAVE WON!" << endl;
                break;
            }

	
			if (!found) //You lose when you get 6 wrong letters
			{
				cout << "Wrong Letter!" << endl;
				
				wrong++;
					if (wrong==6)
					{
						cout << "You are Dead! GameOver!" << endl;
						cout << "Your word was " << word << endl;
						cout << endl;
						break;
					}
			}
			

			cout << "Word:" << letters << endl;
			cout << "Correct:" << correct << endl;
			cout << "Incorrect:" << incorrect << endl;
			cout << endl;
			cout << endl;

			
	}

		

}



int _tmain(int argc, _TCHAR* argv[]) //main
{
	cout << "Let's Play HangMan!" << endl;
	
	srand(time(NULL));

	char playagain = 'Y';



	while (playagain == 'Y')
	{
		string word = LoadFile();
		RunGame(word);
		cout << endl;
		cout << "Play Again? (Y/N)" << endl;
		cin >> playagain;
		playagain = toupper(playagain);
	}

	




	cout << "Thank You for Playing" << endl;
	
	
	_getch();
	return 0;
}



Okay, now two things I noticed. When I'm choosing a random word from the file, it just gives me blank spaces...so when I type in any random letter, it says I win. Why is that? (You'll notice if you do "cout << word << endl; just at the begin of the loop to make sure the loadfile() is working.)

Another thing, how do I make it so the when the user type a letter, Upper or Lower Case, it will match the letter of the word you a trying to guess, Upper or Lower case?

EX: Word is Maine. If they type "m", they guess right. If they type "A", they guess right
Last edited on
What you do is that you toLower() the guess of the input with the letter in the word that you are checking in your for loop:
1
2
3
4
5
6
7
if ((char)tolower(guess) == (char)tolower(word[k]))
{
    cout << "Good Guess!" << endl;
    letters[k] = guess;
    found = true;
    count++;	
}
drue why would you make a loading screen when nothing is being loaded... thats not making the game look more real its just retarded programming.
@ascii hahahaha, i was thinking the same thing but didn't want to say anything. :P
@GodPyro Thanks, you have been of great help.
Last edited on
Topic archived. No new replies allowed.