Hangman Issue

This is a program for a simple hangman game. The issue right now is the out put it gives. If I enter tackle, it ends correctly saying you have won. Right now, with the else statement commented out, it just runs until tackle is entered. When I uncomment the else statement, that is where my problem comes into play. If I type just 't' for my first letter, it will say "that letter is in the word" and then repeat 5 more times saying it is not in the word. It also adds +5 to the tries variable and the next letter you type you instantly lose. Im just not sure where to go from here. Thanks for any help!

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
#include <iostream>
#include <string>

using namespace std;

int main() {

        int tries = 0;
        int right = 0;
        char guess;
        string word = "tackle";
        int length = word.length();  // Sets length to the length of the string variable word
        cout << length << endl;

        cout << "Try to guess the word, you have six guesses!" << endl;

        while(tries <= 6) {

                cout << "Enter a letter: " << endl;
                cin >> guess;
                cout << "You guessed: " << guess << endl;

                for(int i = 0; i < length; i++) {

                        if(word[i] == guess) {        // Letter is in word

                                cout << "This letter is in the word!" << endl;
                                right++;
                        }

                //      else {
                //
                //              cout << "This letter is not in the word :(" << endl;
                //              tries++;
                //      }
                }

                if(right == length) {     // Win

                        cout << "You win!" << endl;
                        cout << "The word was " << word << endl;
                        break;
                }

                if(tries == 6) {          // Lose

                        cout << "You lost!" << endl;
                        cout << "The word was " << word << endl;
                        break;
                }
        }

        return 0;
}
I wrote a similar program sometime ago. Review my code and see if it helps, if you don't understand anything let me know (:

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string answer = "scratch";
	string displayAnswer = "";

	for (int i = 0; i < answer.length(); i++)
	{
		displayAnswer += "-";
	}

	int numGuesses = 0;
	bool keepGoing = true;

	while (keepGoing)
	{
		
		char guess;

		numGuesses++;
		cout << "Enter a letter: ";
		cin >> guess;

		for (int i = 0; i < answer.length(); i++)
		{
			if (guess == answer.at(i))
			{
				displayAnswer = displayAnswer.substr(0, i) + answer.at(i) + displayAnswer.substr(i + 1, answer.length());
			}
		 
		}

		cout << "Display: " << displayAnswer << endl << endl;
		if (displayAnswer == answer)
		{
			cout << "You;ve won! It took " << numGuesses << " guesses!" << endl;
		}
	}
	 
	system("PAUSE");
	return 0;
}
Your if statement is in a for loop. If your else statement commented out your If statement will notify the letter is in the word then it goes back into the for loop and then checks to see if T is the 2nd letter in your word and then a third time and a fourth and then a fifth until every letter of tackle is checked against the T.

There are several solutions to this problem depending on the parameters of the assignment Let me know if you need more help!
Last edited on
Thanks for the replies! Im going to check this out when I get home, Ill let you know what I come up with, good or bad haha.
I just have one more question at the moment, I moved the else statement around, turned it into an if statement trying various spots for different outcomes. I still can't get it to just read one letter at a time. Any suggestions for that? Thanks!
I think there are some flaws with your original code. If the user keeps typing the same letter that is found in "tackle", the "right" variable keeps increasing and you can win the game only by typing the same letter... I've made some changes. Now I think everything is correct. Check it out...

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
#include <iostream>
#include <string>

using namespace std;

int main() {

        int tries = 0;
        int right = 0;
        bool found = false;
        char guess;
        string word = "tackle";
        char guessed[6]; // This string gets all the correct letters you have already typed
        int size = 1;
        int validletter = false; 
        int aux = 0;
        int length = word.length();  // Sets length to the length of the string variable word
        cout << length << endl;


         cout << "Try to guess the word, you have six guesses!" << endl;

        while(tries <= 6) {
                validletter = false;

                if (aux==0)
                {
                        cout << "Enter a 1st letter: ";
                        cin >> guess;
                        guessed[0] = guess;
                        cout << "You guessed: " << guess << endl;

                } else {

                while (validletter==false) // You can only get out of this loop by typing a valid letter. 
                {
                        cout << "\n\nEnter a letter: ";
                        cin >> guess;
                        cout << "You guessed: " << guess << endl;
                        validletter = true;
                        for (int i=0;i<size;i++)
                        {
                                if (guess==guessed[i])
                                {
                                        cout << "\nYou already said that letter. Try again\n";
                                        validletter = false;
                                        i=size;
                                }
                        }
                } }

                aux++;

                for(int i = 0; i < length; i++) {
                        found = false;

                        if(word[i] == guess) {        // Letter is in word

                                cout << "This letter is in the word!" << endl;
                                i=length;
                                found = true;
                                right++;
                                guessed[size] = guess;
                                size++;
                        }

                      else if (i==length-1 && found==false) {
                
                              cout << "This letter is not in the word :(" << endl;
                              tries++;
                      }
                }

                if(right == length) {     // winning condition

                        cout << "You win!" << endl;
                        cout << "The word was " << word << endl;
                        break;
                }

                if(tries == 6 && right!= length) {          // losing condition

                        cout << "You lost!" << endl;
                        cout << "The word was " << word << endl;
                        break;
                }
        } 


        return 0;
}
Last edited on
Thank you, that helped a ton. I didn't even realize that error so thanks for pointing it out!
Topic archived. No new replies allowed.