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:
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.
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!
do
{
constint 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());
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?
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
{
constint 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;
}
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~!