hangman program problem. I'd appreciate some help here.

Hello there,
I'm trying to make a hangman program. I am a beginner, as a result i don't know much about C++ programming etc.
My program has a list of 20 words which each one of them has 10 letters. The program randomly select a word at the beginning and inform the player that he has 10 possibly wrong answers. Well, the problem is that when i type a wrong letter it doesn't display the letter i typed wrong. how do i fix that. The second problem is when i lose and type Y so i can try again. I lose automatically. how do i fix that too? One more problem is that when i type the same wrong letter twice I lose a try. How do i fix that too? The last problem which i do not know how to fix is when i win I would like the program to print something like " congratulations or w/e"
Thank you for your time guys. This 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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

string THE_WORD;            // word to guess
int wrong;
string soFar;
string used;

bool match(char letter, string word);
char askGuess(string usedLettersStr); //tells the compiler of method askGuess
bool playAgain();

int main()
{
    srand(time(0));

    vector<string> words;  // collection of possible words to guess
    words.push_back("ACHIVEMENT");
    words.push_back("ABDICATION");
    words.push_back("ABSOLUTELY");
    words.push_back("BLACKJACKS");
    words.push_back("MAXIMIZING");
    words.push_back("JACKHAMMER");
    words.push_back("OBJECTIVES");
    words.push_back("MOBILIZING");
    words.push_back("HYPNOTIZED");
    words.push_back("EMPATHIZED");
    words.push_back("EXCELLENCY");
    words.push_back("EMPHASIZED");
    words.push_back("VAPORIZING");
    words.push_back("BANKRUPTCY");
    words.push_back("PREJUDICED");
    words.push_back("PROJECTING");
    words.push_back("FREQUENTLY");
    words.push_back("EXPENDABLE");
    words.push_back("BLACKBERRY");
    words.push_back("PROGRAMING");

    cout << "Welcome to Hangman.  Good luck!\n";

 // loop starts here
 bool done = false;
 do
 {
    const int MAX_WRONG = 10;  // maximum number of incorrect guesses allowed

    random_shuffle(words.begin(), words.end());
    THE_WORD = words[0];            // word to guess

    soFar = string(THE_WORD.size(), '-');          // word guessed so far
    used = "";                            // letters already guessed

    // loop for current word
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
        cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "\nSo far, the word is:\n" << soFar << endl;

        used += askGuess(used);



    } // end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD))

    // shut down
    if (wrong == MAX_WRONG)
    {
      cout << "\nYou lost, as a result you've been hanged!";
    }

    cout << "\nThe word is " << THE_WORD << endl;

} while(playAgain());

return 0;
}

inline bool match(char letter, string word)
{
       return ( word.find(letter) != string::npos );
}

char askGuess(string usedLettersStr)
{
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //make uppercase since secret word in uppercase
//        while (used.find(guess) != string::npos)
        while (match(guess, used))
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }


//        if (THE_WORD.find(guess) != string::npos)
        if (match(guess, THE_WORD))
        {
            cout << "That's right! " << guess << " is in the word.\n";

            // update soFar to include newly guessed letter
            for (int i = 0; i < THE_WORD.length(); ++i)
                if (THE_WORD[i] == guess)
                    soFar[i] = guess;
        }
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++wrong;
        }
}

bool playAgain() // function to play again while clearing system
{
     char again;
     cout << "\n\nWould you like to play again? <y/n>: ";
     cin >> again;

     cin.clear(); //clear and ignore cin
     cin.ignore();

     again = toupper(again);

     system("cls");

     return (again == 'Y');
}
Last edited on
@Tili

The reason it doesn't display the typed letters, is because you don't return the guess in the askGuess function. Just add return guess; before the last closing bracket in the function. You should also add wrong = 0; to reset the wrong guesses when starting a new game, so you don't lose to fast. Also, your srand(time(0));, should look like srand((unsigned)time(0));. The other stuff your wondering about, shouldn't be to hard to fix. Work on it a bit more, and ask again if you are still stuck.

Last thing, Programming has to "M"'s and the top word is spelled"ACHIEVEMENTS". It's okay to have varied word lengths.
@whitenite1

Nice eye you got there. Thank you very much but as I said earlier I am a beginner and this is a project for my university class. I dont really want to learn those stuff. It's way to hard for me and programming language need a lot of time which I can not afford right now. So If you can it will be handy to just tell me exactly what line should i put what. erase/delete/transfer
I would have done it alone (+ your post ) but I really dont have the knowledge nor am i capable of doing it. So if you have time and willing to spend it helping me that would be great! :D
Thank you for your fast response. Have a wonderful day!
@Tili

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
do
 {
    const int MAX_WRONG = 10;  // maximum number of incorrect guesses allowed
    wrong = 0; // Add here. Resets to 0, how many wrong guesses you have.

    random_shuffle(words.begin(), words.end());
    THE_WORD = words[0];            // word to guess

    soFar = string(THE_WORD.size(), '-');          // word guessed so far
    used = "";                            // letters already guessed

    // loop for current word
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
        cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "\nSo far, the word is:\n" << soFar << endl;

        used += askGuess(used);



    } // end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD))

    // shut down
    if (wrong == MAX_WRONG)
    {
      cout << "\nYou lost, as a result you've been hanged!";
    }

    cout << "\nThe word is " << THE_WORD << endl;

} while(playAgain());


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
char askGuess(string usedLettersStr)
{
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //make uppercase since secret word in uppercase
//        while (used.find(guess) != string::npos)
        while (match(guess, used))
        {
            cout << "\nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }


//        if (THE_WORD.find(guess) != string::npos)
        if (match(guess, THE_WORD))
        {
            cout << "That's right! " << guess << " is in the word.\n";

            // update soFar to include newly guessed letter
            for (int i = 0; i < THE_WORD.length(); ++i)
                if (THE_WORD[i] == guess)
                    soFar[i] = guess;
        }
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++wrong;
        }
return guess; // Add here. Returns the char user entered for letter in word 
}
Sorry to bother dude but I need help with mine too. do you know how to put a limit to the hangman game I have everything working I just need that part. I would really appreciate if I get a little help.
@alcon92
You should open a new topic and ask for answers. I dont mind that you wrote here but its confusion for all the other folks who are trying to read and pbly answer my problem.

@Whitenite1

It worked. Thank you very much for your help and effort. You rock! :D :D :D
I dont want to bother you anymore but I have a last question. How do i make the program to show "congratulations! You Won" after someone find successfully the hidden word ?
What line should i put what?
@Tili

Here's a new main function, with the "Congratulations.." in it. Just replace your main with it.

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
int main()
{
	srand((unsigned)time(0));
	int len;
	vector<string> words; // collection of possible words to guess
	words.push_back("ACHIEVEMENTS");
	words.push_back("ABDICATION");
	words.push_back("ABSOLUTELY");
	words.push_back("BLACKJACKS");
	words.push_back("MAXIMIZING");
	words.push_back("JACKHAMMER");
	words.push_back("OBJECTIVES");
	words.push_back("MOBILIZING");
	words.push_back("HYPNOTIZED");
	words.push_back("EMPATHIZED");
	words.push_back("EXCELLENCY");
	words.push_back("EMPHASIZED");
	words.push_back("VAPORIZING");
	words.push_back("BANKRUPTCY");
	words.push_back("PREJUDICED");
	words.push_back("PROJECTING");
	words.push_back("FREQUENTLY");
	words.push_back("EXPENDABLE");
	words.push_back("BLACKBERRY");
	words.push_back("PROGRAMMING");

	cout << "Welcome to Hangman. Good luck!\n";

	// loop starts here
	bool done = true;
	do
	{
		const int MAX_WRONG = 10; // maximum number of incorrect guesses allowed

		random_shuffle(words.begin(), words.end());
		THE_WORD = words[0]; // word to guess
		wrong = 0;
		soFar = string(THE_WORD.size(), '-'); // word guessed so far
		used = ""; // letters already guessed

		// loop for current word
		while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
		{
			cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
			cout << "\nYou've used the following letters:\n" << used << endl;
			cout << "\nSo far, the word is:\n" << soFar << endl;

			used += askGuess(used);
		} // end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
		// shut down
		if (wrong == MAX_WRONG)
		{
			cout << "\nYou lost, as a result you've been hanged!";
		}
		len = THE_WORD.length();
		for (int x = 0; x < len; x++)
		{
			if (soFar[x] == '-')
				done = false;
		}
		if (done)
			cout << "Congratulations!! You finished the word, with " << MAX_WRONG - wrong << " wrong guesses left.." << endl;

		cout << "\nThe word was " << THE_WORD << endl;

	} while (playAgain());

	return 0;
}
@whitenite1

Thank you my man but i figure it out my self this morning. I didnt want to bother you anymore so i did a little digging and I manage somehow to make it work the way i want.You have been a great help to me thank you for your tips and time :D See Ya~!
@Tili

Great to hear. Congrats.
Topic archived. No new replies allowed.