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!>>>
// Hangman
// The classic game of hangman, now with methods
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
usingnamespace 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
{
constint 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;
}
inlinebool 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');
}
So constint 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?
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.
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.