C++ hangman program (stuck on one problem)

Pages: 12
Goal: Create a Hangman program to guess a 5 letter word.
More details:
-Users gets 5 letter guesses, and 5 word guesses.
-NO switches, loops, goto statements, etc. (Mainly if and else statements"
-IF the user enters more than 1 letter during a letter guess, the program should break.

My problem is:
"Enter your first letter guess: " and I enter "a" // everything works fine.

But when "Enter your first letter guess: " and I enter "aaa" // It should output "Bad input, please run program again" and break.

"bad input. please run program again. BUT instead of breaking
it also output "enter first word guess".

How do i stop this from happening?

***I believe the problem starts at line 43, and ends at the bottom


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
 
int main()
{
    string player1;
    string player2;
    string dashes = "_ _ _ _ _";
    string word, letter, letterguess, guess, wrong;
    string wordguess1, wordguess2, wordguess3, wordguess4, wordguess5;    
    bool validLetterGuess = true;
    bool GameOver = false;

    //Intro
    cout << "----------------------HANGMAN!--------------------" << endl;
    cout << "==================================================" << endl;

    //Player name entry // and word selection//

    cout << "Player 1, please enter your name: " << endl;
    cin >> player1;
    cout << "Player 2, please enter your name: " << endl;
    cin >> player2;
    cout << "Ok " << player1 << " and " << player2 << ". Let's start the game!" << endl;
    cout << endl;

    cout << player1 << " please input the 5 letter word you want " << player2 << " to guess." << endl;
    cin >> word;

    //Round 1
    if (word.length() == 5) // IF the length of the word IS EQUAL TO (==) 5:
    {
        word.at(0) = tolower(word.at(0)); //
        word.at(1) = tolower(word.at(1)); //
        word.at(2) = tolower(word.at(2)); /// IF word AT (.at) position (x) = tolower word.at(x) / if word at (x) is "A", tolower word.at(x) is "a"
        word.at(3) = tolower(word.at(3)); //
        word.at(4) = tolower(word.at(4)); //

        cout << "You have 5 guesses. Enter your first letter guess: " << endl; //User starts with 5 guesses. This is the first letter guess.
        cin >> letterguess;

        letterguess.at(0) = tolower(letterguess.at(0));


        if (letterguess.length() != 1) // IF the letter guess IS NOT EQUAL TO (!=) 1: Then it is a bad input. Run the program again. 
        {
            cout << "You are only supposed to input 1 letter. Run program again." << endl;
            validLetterGuess = false;
         
        }
        if (letterguess.at(0) == word.at(0) && validLetterGuess == true)
        {
            cout << "You guessed the first letter right!" << endl;
            dashes.at(0) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(1) && validLetterGuess == true)
        {
            cout << "You guessed the second letter right!" << endl;
            dashes.at(2) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(2) && validLetterGuess == true)            // Round 1
        {
            cout << "You guessesd the third letter right!" << endl;
            dashes.at(4) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(3) && validLetterGuess == true)
        {
            cout << "You guessed the fourth letter right!" << endl;
            dashes.at(6) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(4) && validLetterGuess == true)
        {
            cout << "You guessed the fifth letter right!" << endl;
            dashes.at(8) = letterguess.at(0);
        }
        if (validLetterGuess == true)
        {
            cout << dashes << endl;
            cout << "You have 5 guesses. Enter your first word guess : " << endl;
            cin >> wordguess1;
        }
    }

    if (wordguess1 == word)
    {
        GameOver = true;
        cout << "You guessed the word correctly in ONE guess! YOU WIN!!" << endl;
    }
Last edited on
You should post full, compileable code.

Are you sure you're not allowed to use any form of loop? That's really what your prompt says, verbatim? It's a bizarre, counter-productive requirement.
Last edited on
It will not allow me to post. Since it's all if else statements it is a lot of lines of code. I pass the limit.
you need an else block.

if (letterguess.length() != 1)
{
blah blah bad user
}
else
{
if
if
if... etc
}

prefer [] over .at unless you want to catch the errors thrown by .at... [] is notably faster.

you will probably end up in a loop after you consider it for a bit, though, not the else block. ?? I am unsure where you are going so I will suggest:

while(letterguess.length() != 1)
{
complain and re-enter
}
//here, its good, play the round out
Last edited on
What is the exact input that causes an issue?

This works OK for me:
Player 1, please enter your name: 
Steve
Player 2, please enter your name: 
Sven
Ok Steve and Sven. Let's start the game!

Steve please input the 5 letter word you want Sven to guess.
apple
You have 5 guesses. Enter your first letter guess: 
aaa
You are only supposed to input 1 letter. Run program again.
jonnin

I tried this. It still will not work.
post what you tried, in full, in a new post, please.
and check to make sure I didn't edit as you were reading. I tend to revise a little.
Last edited on
Ganado

----------------------HANGMAN!--------------------
==================================================
Player 1, please enter your name:
Bob
Player 2, please enter your name:
John
Ok Bob and John. Let's start the game!

Bob please input the 5 letter word you want John to guess.
Tests
You have 5 guesses. Enter your first letter guess:
aaa
You are only supposed to input 1 letter. Run program again.
You have 4 guesses left. Enter your second letter guess:


this is what I get. I need it to NOT output "Enter your second letter guess"
Last edited on
so this is not working:


while(letterguess.length() != 1)
{
complain and re-enter
}
//here, its good, play the round out

?
FULL CODE (2 parts)

[[Rounds 1 - 3]]

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
using namespace std;

int main()
{
    string player1;
    string player2;
    string dashes = "_ _ _ _ _";
    string word, letter, letterguess, guess, wrong;
    string wordguess1, wordguess2, wordguess3, wordguess4, wordguess5;    
    bool validLetterGuess = true;
    bool GameOver = false;

    //Intro
    cout << "----------------------HANGMAN!--------------------" << endl;
    cout << "==================================================" << endl;

    //Player name entry // and word selection//

    cout << "Player 1, please enter your name: " << endl;
    cin >> player1;
    cout << "Player 2, please enter your name: " << endl;
    cin >> player2;
    cout << "Ok " << player1 << " and " << player2 << ". Let's start the game!" << endl;
    cout << endl;

    cout << player1 << " please input the 5 letter word you want " << player2 << " to guess." << endl;
    cin >> word;

    //Round 1
    if (word.length() == 5) // IF the length of the word IS EQUAL TO (==) 5:
    {
        word.at(0) = tolower(word.at(0)); //
        word.at(1) = tolower(word.at(1)); //
        word.at(2) = tolower(word.at(2)); /// IF word AT (.at) position (x) = tolower word.at(x) / if word at (x) is "A", tolower word.at(x) is "a"
        word.at(3) = tolower(word.at(3)); //
        word.at(4) = tolower(word.at(4)); //

        cout << "You have 5 guesses. Enter your first letter guess: " << endl; //User starts with 5 guesses. This is the first letter guess.
        cin >> letterguess;

        letterguess.at(0) = tolower(letterguess.at(0));


        if (letterguess.length() != 1) // IF the letter guess IS NOT EQUAL TO (!=) 1: Then it is a bad input. Run the program again. 
        {
            cout << "You are only supposed to input 1 letter. Run program again." << endl;
            validLetterGuess = false;
         
        }
        if (letterguess.at(0) == word.at(0) && validLetterGuess == true)
        {
            cout << "You guessed the first letter right!" << endl;
            dashes.at(0) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(1) && validLetterGuess == true)
        {
            cout << "You guessed the second letter right!" << endl;
            dashes.at(2) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(2) && validLetterGuess == true)            // Round 1
        {
            cout << "You guessesd the third letter right!" << endl;
            dashes.at(4) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(3) && validLetterGuess == true)
        {
            cout << "You guessed the fourth letter right!" << endl;
            dashes.at(6) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(4) && validLetterGuess == true)
        {
            cout << "You guessed the fifth letter right!" << endl;
            dashes.at(8) = letterguess.at(0);
        }
        if (validLetterGuess == true)
        {
            cout << dashes << endl;
            cout << "You have 5 guesses. Enter your first word guess : " << endl;
            cin >> wordguess1;
        }
    }

    if (wordguess1 == word)
    {
        GameOver = true;
        cout << "You guessed the word correctly in ONE guess! YOU WIN!!" << endl;
    }
    //ROUND 2
    if (GameOver == false)
    {
        cout << "You have 4 guesses left. Enter your second letter guess: " << endl;
        cin >> letterguess;

        letterguess.at(0) = tolower(letterguess.at(0));


        if (letterguess.length() != 1) // IF the letter guess IS NOT EQUAL TO (!=) 1: Then it is a bad input. Run the program again. 
        {
            cout << "You are only supposed to input 1 letter. Run program again." << endl;
            validLetterGuess = false;
        }
        if (letterguess.at(0) == word.at(0) && validLetterGuess == true)
        {
            cout << "You guessed the first letter right!" << endl;
            dashes.at(0) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(1) && validLetterGuess == true)
        {
            cout << "You guessed the second letter right!" << endl;
            dashes.at(2) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(2) && validLetterGuess == true)            // Round 1
        {
            cout << "You guessesd the third letter right!" << endl;
            dashes.at(4) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(3) && validLetterGuess == true)
        {
            cout << "You guessed the fourth letter right!" << endl;
            dashes.at(6) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(4) && validLetterGuess == true)
        {
            cout << "You guessed the fifth letter right!" << endl;
            dashes.at(8) = letterguess.at(0);
        }
        if (validLetterGuess == true)
        {
            cout << dashes << endl;
            cout << "You have 4 guesses. Enter your second word guess : " << endl;
            cin >> wordguess2;
        }
    }
    if (wordguess2 == word)
    {
        GameOver = true;
        cout << "You guessed the word correctly in TWO guesses! YOU WIN!!" << endl;
    }
    //ROUND 3
    if (GameOver == false)
    {
        cout << "You have 3 guesses left. Enter your third letter guess: " << endl;
        cin >> letterguess;

        letterguess.at(0) = tolower(letterguess.at(0));


        if (letterguess.length() != 1) // IF the letter guess IS NOT EQUAL TO (!=) 1: Then it is a bad input. Run the program again. 
        {
            cout << "You are only supposed to input 1 letter. Run program again." << endl;
            validLetterGuess = false;
        }
        if (letterguess.at(0) == word.at(0) && validLetterGuess == true)
        {
            cout << "You guessed the first letter right!" << endl;
            dashes.at(0) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(1) && validLetterGuess == true)
        {
            cout << "You guessed the second letter right!" << endl;
            dashes.at(2) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(2) && validLetterGuess == true)            // Round 1
        {
            cout << "You guessesd the third letter right!" << endl;
            dashes.at(4) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(3) && validLetterGuess == true)
        {
            cout << "You guessed the fourth letter right!" << endl;
            dashes.at(6) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(4) && validLetterGuess == true)
        {
            cout << "You guessed the fifth letter right!" << endl;
            dashes.at(8) = letterguess.at(0);
        }
        if (validLetterGuess == true)
        {
            cout << dashes << endl;
            cout << "You have 3 guesses. Enter your third word guess : " << endl;
            cin >> wordguess3;
        }
       
    }
    if (wordguess3 == word)
    {
        GameOver = true;
        cout << "You guessed the word correctly in THREE guesses! YOU WIN!!" << endl;
    }
[[Rounds 4 and 5]]

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
//Round 4
    if (GameOver == false)
    {
        cout << "You have 2 guesses left. Enter your fourth letter guess: " << endl;
        cin >> letterguess;

        letterguess.at(0) = tolower(letterguess.at(0));


        if (letterguess.length() != 1) // IF the letter guess IS NOT EQUAL TO (!=) 1: Then it is a bad input. Run the program again. 
        {
            cout << "You are only supposed to input 1 letter. Run program again." << endl;
            validLetterGuess = false;
        }
        if (letterguess.at(0) == word.at(0) && validLetterGuess == true)
        {
            cout << "You guessed the first letter right!" << endl;
            dashes.at(0) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(1) && validLetterGuess == true)
        {
            cout << "You guessed the second letter right!" << endl;
            dashes.at(2) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(2) && validLetterGuess == true)            // Round 1
        {
            cout << "You guessesd the third letter right!" << endl;
            dashes.at(4) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(3) && validLetterGuess == true)
        {
            cout << "You guessed the fourth letter right!" << endl;
            dashes.at(6) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(4) && validLetterGuess == true)
        {
            cout << "You guessed the fifth letter right!" << endl;
            dashes.at(8) = letterguess.at(0);
        }
        if (validLetterGuess == true)
        {
            cout << dashes << endl;
            cout << "You have 2 guesses. Enter your fourth word guess : " << endl;
            cin >> wordguess4;
        }
    }
    if (wordguess4 == word)
    {
        GameOver = true;
        cout << "You guessed the word correctly in FOUR guesses! YOU WIN!!" << endl;
    }
    //Round 5
    if (GameOver == false)
    {
        cout << "You have 1 guess left. Enter your final letter guess: " << endl;
        cin >> letterguess;

        letterguess.at(0) = tolower(letterguess.at(0));


        if (letterguess.length() != 1) // IF the letter guess IS NOT EQUAL TO (!=) 1: Then it is a bad input. Run the program again. 
        {
            cout << "You are only supposed to input 1 letter. Run program again." << endl;
            validLetterGuess = false;
        }
        if (letterguess.at(0) == word.at(0) && validLetterGuess == true)
        {
            cout << "You guessed the first letter right!" << endl;
            dashes.at(0) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(1) && validLetterGuess == true)
        {
            cout << "You guessed the second letter right!" << endl;
            dashes.at(2) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(2) && validLetterGuess == true)            // Round 5
        {
            cout << "You guessesd the third letter right!" << endl;
            dashes.at(4) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(3) && validLetterGuess == true)
        {
            cout << "You guessed the fourth letter right!" << endl;
            dashes.at(6) = letterguess.at(0);
        }
        if (letterguess.at(0) == word.at(4) && validLetterGuess == true)
        {
            cout << "You guessed the fifth letter right!" << endl;
            dashes.at(8) = letterguess.at(0);
        }
        if (validLetterGuess == true)
        {
            cout << dashes << endl;
            cout << "You have 1 guesses. Enter your final word guess : " << endl;
            cin >> wordguess5;
        }
    }
    if (wordguess5 == word)
    {
        GameOver = true;
        cout << "You guessed the word correctly in FIVE guesses! YOU WIN!!" << endl;
    }
    else if (wordguess5 != word)
    {
        GameOver = true;
        cout << "YOU LOSE. Better luck next time!" << endl;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Ok, apart from the loop on the valid letter that I suggested, you may also want to loop the ROUNDS.  

so the overall flow looks like
intro
rounds = 0;
won = false;
do
{
	do
	{
	prompt
	get input  ​
	}(while input length != 1)
	all your round logic (won set true if you win here)	
	rounds++;
}while(rounds < 5 && !won); //5 should maybe be a named constant. 
jonnin

we are not allowed to utilize: loops, switches, goto statements, etc. This project was to get us used to "if else" statements.
Ok ... what about functions?
There is no SANE way to handle an idiot user that types XXXXX every time 50 times in a row as the guess without a loop, unless you do what you already did, which is to charge them a guess for their stupidity and keep going. You can do it with a function and recursion without any of the listed bans, but that is just semantics (its a hidden loop) and you can probably rig up a <algorithm> type hidden loop in some of the exotic c++ toys but ??? I dunno what else to tell you. You can't code without basic constructs like a loop.
Here are the instructions directly from the teacher:

Notes:
• You cannot use any kind of loop.
• Any function besides .at(), toUpper(), toLower(), any cin functions we learned in chapter 3, or .length() need to be approved by me before you can use them.
• Once you get the first 2 letter guesses done you should be able to copy and paste.
• Use lots of Boolean variables.
• You cannot use goto statements
• You cannot use multiple return 0 statements
• You must treat uppercase letters and lowercase letters as the same. Meaning if the word to guess is “Hello” and the user guesses ‘h’ the program should still recognize that the letter was guessed correctly.
Okay, uh, tl;dr -- I just assumed you were missing a few lines of code. This requirement of not having loops/unapproved functions is ridiculous. However, if the problem, as I understand, is that you want it to display "X", but it's displaying "X Y", then return from main immediately after displaying X.

e.g.
1
2
3
4
5
6
7
if (whatever)
{
   X;
   return 1;
}
// ...
Y;
Last edited on
Yeah its rough lol. Cant have multiple return statements either. So that's why I'm so lost. I have no idea. I might just turn it in as is, or take it to him tomorrow before I turn it in and ask him.
Then just have a magic bool condition that says "dont_do_anything_else = true;" and check for !dont_do_anything_else before it prints the thing you don't want it to print.

PS: Your text says "cannot have multiple return 0 statements" but I returned 1, not 0.
Last edited on
nothing about how to handle bad input in those
maybe you had it right for what he wanted.
Bad input is not part of the homework. Assume good input.
Pages: 12