C++ hangman program (stuck on one problem)

Pages: 12
-IF the user enters more than 1 letter during a letter guess, the program should break.
So that requirement no longer applies?
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
#include <iostream>
#include <string>

int main()
{
    std::string word{"abcde"};
    char guess{};
    
    bool found1{false};
    std::cout << "Please guess a character: ";
    std::cin >> guess;
    guess = toupper(guess);
    
    if(toupper(word.at(0)) == guess)
    {
        found1 = true;
    }
    else if(toupper(word.at(1)) == guess)
    {
        found1 = true;
    }
    else if(toupper(word.at(2)) == guess)
    {
        found1 = true;
    }
    else if(toupper(word.at(3)) == guess)
    {
        found1 = true;
    }
    else if(toupper(word.at(4)) == guess)
    {
        found1 = true;
    }
    else
        found1 = false;
    
    
    
    
    bool found2{false};
    std::cout << "Please guess a character: ";
    std::cin >> guess;
    guess = toupper(guess);
    
    if(toupper(word.at(0)) == guess)
    {
        found2 = true;
    }
    else if(toupper(word.at(1)) == guess)
    {
        found2 = true;
    }
    else if(toupper(word.at(2)) == guess)
    {
        found2 = true;
    }
    else if(toupper(word.at(3)) == guess)
    {
        found2 = true;
    }
    else if(toupper(word.at(4)) == guess)
    {
        found2 = true;
    }
    else
        found2 = false;
    
    
    // ... etc etc etc
    
    
    if( (found1 == false) || (found2 == false)/* blah blah */)
        std::cout << "Bad\n";
    else
        std::cout << "Excellent\n";
    
    return 0;
}


Of course, if you nest each stage even deeper then the first character to produce a false result is enough to escape and reach the one and only return statement.
Last edited on
Hey guys, sorry for getting back so late but I have found the solution. I wanted to thank you all for taking the time to try and help me out, I truly appreciate it. The fix was a simple one, and it made me feel really dumb not figuring it out sooner but here it is.

UPDATE: SOLVED.

1
2
3
4
5
//ROUND 2
    if (GameOver == false)
    {
        cout << "You have 4 guesses left. Enter your second letter guess: " << endl;
        cin >> letterguess;


the simple fix:

1
2
3
4
5
//ROUND 2
    if (GameOver == false && validLetterGuess == true)
    {
        cout << "You have 4 guesses left. Enter your second letter guess: " << endl;
        cin >> letterguess;



works like a charm. Breaks when more than one letter is entered, as it should. Hopefully this will help someone else, or enlighten us. Thanks again for all the help!
Topic archived. No new replies allowed.
Pages: 12