Help with C++ Hangman Assignment

The instructions are this:::
Rewrite the Hangman game (Chapter 4) using functions. Include a function to get the player’s guess and another function to determine whether the player’s guess is in the secret word.

The code compiles with no errors BUT there is a logic error somewhere. The letters guessed that ARE in the word run into the loop of "you've already guessed <<guess<< when infact you haven't.. BUT if you enter that same letter again it accepts the letter and moves on correctly until your next guess.


// Hangman
// The classic game of hangman

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;

char getGuess(string &used); //Prototype Function Declaration
char isCorrect(); //Prototype Function Declaration

using namespace std;

int main()
{
// set-up
const int MAX_WRONG = 10; // maximum number of incorrect guesses allowed

vector<string> words; // collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");

srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong = 0; // number of incorrect guesses
string soFar(THE_WORD.size(), '-'); // word guessed so far
string used = ""; // letters already guessed

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

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


char guess = getGuess(used);

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

// update soFar to include newly guessed letter
for (unsigned 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;
}
}

// shut down
if (wrong == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";

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

return 0;
}

//----getGuess Function Return starts here---

char getGuess(string &used)
{
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); //if guess IS in the string, ask for guess again in while loop
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
return guess;
}


Please help - assignment is due this week...ARRGH!!
Last edited on
In this post you might see what you are missing. The code is slightly different than yours but it has the same general starting point. Look for guessAWord() and GuessALetter() functions. They may be what you are looking for.

http://www.cplusplus.com/forum/general/42463/2/
while (used.find(guess) != string::npos); //if guess IS in the string, ask for guess again in while loop

You should not have a semicolon there. That seems to be what causes the problem you're describing.
That was it!! the semicolon was not needed.
Thanks for all your help!

Sagar
Topic archived. No new replies allowed.