(SOLVED) Hangman game. Works. Compiles. Yet, there's an error!

Apr 3, 2009 at 12:52am
Hey guys,
I created a hangman game. The assignment was to redo the hangman game that was given in our book and to reconstruct it using methods.
Which I did. Everything works, and compiles just fine.
BUT, there's a mistake somewhere. And I can't put my finger on it.
So I am requesting your mighty help to solve this - most likely - easy problem. The problem occurs where the code is bold. What did I do wrong?
<<<More to read after the code itself!>>>

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
// Hangman
// The classic game of hangman, now with methods

#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("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");
    words.push_back("BOOK");
    words.push_back("DIGITAL");
    words.push_back("PROGRAM");

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

 // loop starts here
 bool done = false;
 do
 {
    const int MAX_WRONG = 8;  // 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've been hanged!";
    }

    cout << "\nThe word was " << 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');
}


Now let me show you an image.
http://img.photobucket.com/albums/v244/fudgeo/hangman_error.jpg
[Note that this picture isn't the full running executable file, just a segment]
Please read inside the read box and just admire the Twilight Zone. haha

Help?
Last edited on Apr 3, 2009 at 2:58am
Apr 3, 2009 at 1:46am
You have to initialize the wrong variable to zero at the start of every game.
Apr 3, 2009 at 1:50am
So const int MAX_WRONG = 8;
should be inside my Game Loop?

Just tried that, seems to make somewhat of a difference, but when I input the letter 'D' for example, it says that I have used 'L'. Why is it doing this?

Did I make a mistake when referencing to used
Did I define used mistakenly?
Last edited on Apr 3, 2009 at 1:53am
Apr 3, 2009 at 1:58am
Okay, I debugged it and found your problem.

In the function askGuess, return guess.
return guess;

Constants shouldn't be in loops but it doesn't matter.

Edit:
I'm assuming you are doing this on a junky compiler in school. I suggest that you install Code::blocks or VC++ on your home computer. Your compiler should have caught the missing return value.

I have to use a piece of crap compiler in school, but when class is over I put the code on my flash drive if it isn't working and debug it at my house.
Last edited on Apr 3, 2009 at 2:01am
Apr 3, 2009 at 2:58am
Yea, I'm using Dev C++. I thought it was pretty decent so far for a basic IDE. Are Code::blocks and VC++ freewares?

Thanks a lot for your debugging.
Now it works just fine!
Apr 3, 2009 at 9:08pm
I have to use an old version of Dev C++ at school and I hate it with a passion. The compiler is not up to the 03 standard so <sstream> and <limits> are not available. Also I like clean code and Dev C++ produces the opposite of clean code. The tabs are screwed up.

Code::blocks is free.
Visual C++ Express 2008 is free as well.

Just google them.

I suggest VC++ for debugging code. All you have to do is click on the side margin and a breakpoint is placed. Then hit the debug key and look at the values of variables, expressions, etc.
Last edited on Apr 3, 2009 at 9:11pm
Topic archived. No new replies allowed.