Making a word guessing puzzle?

Oct 4, 2012 at 2:34am
I am supposed to make a program that tells how many letters are in the word "hangman". And displays "You lose" after six wrong guesses and "You Win!" after guessing all the correct letters. This is what I have so far and I don't know where to go from here. I would be most appreciative if anyone could help.

#include <iostream>

using namespace std;

int main()
{ char letter;
try;
while (try <= 6)
cout << "Guess a letter" << endl;
cin >> letter;
if (letter == 'h' || letter == 'g' || letter == 'm'){
cout << "There is one." << endl;
}
else if (letter == 'a' || letter == 'n'){
cout << "There is two." << endl;
}
else {
try++
cout << "There are none." << endl;
}


}
Oct 4, 2012 at 2:43am
try is a reserved keyword. Use a different name for your variable.
Oct 4, 2012 at 3:03am
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
#include <iostream>
using namespace std;

int main()
{
    char letter;
    int guessNumber = 0;

    while (guessNumber < 6)
    {
        cout << "You have " << 6-guessNumber << " guesses left.\n";
        cout << "Guess a letter: ";
        cin >> letter;

        if (letter == 'h' || letter == 'g' || letter == 'm')
        {
           cout << "There is one." << endl;
           ++guessNumber;
        }

        else if (letter == 'a' || letter == 'n')
        {
           cout << "There is two." << endl;
           ++guessNumber;
        }

        else
        {
           ++guessNumber;
           cout << "There are none." << endl;
        }
    }
}
Oct 4, 2012 at 3:38am
I'm still wondering how to set it to say "You Win!" after guessing the letters h,a,n,g, and m. And "You Lose" after exceeding six guesses.
Oct 4, 2012 at 3:42am
And also, the number of tries only diminishes when guessing correct letters.
Oct 4, 2012 at 3:44am
Never mind, fixed the number of tries issue. Still trying to figure out the "you win" and "you lose".
Oct 4, 2012 at 3:48am
the number of tries only diminishes when guessing correct letters.


So if a user enters 'a' (there are two in the word), you lose a guess?
And if you enter 'x', you don't lose a guess?
In that case, you could have an unlimited amount of guesses if you keep guessing incorrectly.
Last edited on Oct 4, 2012 at 3:49am
Oct 4, 2012 at 3:49am
Okay, I've figure out how to get the "You Lose!" to display. Still haven't figured out the "You Win!".
Oct 4, 2012 at 3:58am
You could do this. Might not be the most efficient, but it works. I don't have the 'you lose' in this snippit.

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
#include <iostream>
using namespace std;

int main()
{
    char letter;
    int guessNumber = 0;
    bool h1 = false;
    bool g1 = false;
    bool m1 = false;
    bool a1 = false;
    bool n1 = false;

    while (guessNumber < 6)
    {
        if (h1 && g1 && m1 && a1 && n1)
        {
            cout << "You Win!";
            return 0;
        }

        cout << "You have " << 6-guessNumber << " guesses left.\n";
        cout << "Guess a letter: ";
        cin >> letter;

        if (letter == 'h')
        {
           cout << "There is one." << endl;
           h1 = true;

        }

        else if (letter == 'g')
        {
           cout << "There is one." << endl;
           g1 = true;

        }

        else if (letter == 'm')
        {
           cout << "There is one." << endl;
           m1 = true;

        }

        else if (letter == 'n')
        {
           cout << "There are two." << endl;
           n1 = true;

        }

        else if (letter == 'a')
        {
           cout << "There are two." << endl;
           a1 = true;

        }

        else
        {
           ++guessNumber;
           cout << "There are none." << endl;
        }
    }
}
Oct 4, 2012 at 4:10am
I tried adding that on to what I had and it worked. Thank You.
Topic archived. No new replies allowed.