program compiles, but doesn't work

Hi all,

Very new to C++... trying to create a hangman program using a char and bool function. Have been working on it for hours and after getting it to compile, I try to run the program and it tells me it's not working! Any help would be greatly appreciated... here's my code:

// Hangman
// The classic game of hangman

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;
char askGuess(string);
bool match (char guess, string THE_WORD);

const int MAX_WRONG = 8;
vector<string> words;
const string THE_WORD = words[0];
int wrong = 0;
string soFar(THE_WORD.size(), '-');
string usedLettersStr = "";

int main()
{
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");

srand(time(0));
random_shuffle(words.begin(), words.end());

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

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

askGuess(usedLettersStr);
}

if (wrong == MAX_WRONG)
{
cout << "\nYou've been hanged!\n";
}
else
{
cout << "\nYou guessed it!";
}

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

return 0;
}



char askGuess(string usedLettersStr)
{
string usedLetterStr;
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);

while ( match(guess, usedLettersStr))
{
cout << "\nYou've already guess " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}

usedLettersStr += guess;

if ( match(guess, THE_WORD))
{
cout << "That's rightt! " << guess << " is in the word.\n";

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;
}
}
}

inline bool match(char guess, string THE_WORD)
{
return (THE_WORD.find(guess));
}

basically, I'm suppose to create a function to retrieve the user's guess... Again, I'd be very grateful for any insight, as I really have no idea why it's not working even though it's compiling....

thanks!!!
Sorry I can't help but you should put your code inbetween [code][/code] It'll be easier to read. Maybe you could insert some cout's to try to isolate the problem.
Topic archived. No new replies allowed.