Goal: Create a Hangman program to guess a 5 letter word.
More details:
-Users gets 5 letter guesses, and 5 word guesses.
-NO switches, loops, goto statements, etc. (Mainly if and else statements"
-IF the user enters more than 1 letter during a letter guess, the program should break.
My problem is:
"Enter your first letter guess: " and I enter "a" // everything works fine.
But when "Enter your first letter guess: " and I enter "aaa" // It should output "Bad input, please run program again" and break.
"bad input. please run program again. BUT instead of breaking
it also output "enter first word guess".
How do i stop this from happening?
***I believe the problem starts at line 43, and ends at the bottom
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
|
int main()
{
string player1;
string player2;
string dashes = "_ _ _ _ _";
string word, letter, letterguess, guess, wrong;
string wordguess1, wordguess2, wordguess3, wordguess4, wordguess5;
bool validLetterGuess = true;
bool GameOver = false;
//Intro
cout << "----------------------HANGMAN!--------------------" << endl;
cout << "==================================================" << endl;
//Player name entry // and word selection//
cout << "Player 1, please enter your name: " << endl;
cin >> player1;
cout << "Player 2, please enter your name: " << endl;
cin >> player2;
cout << "Ok " << player1 << " and " << player2 << ". Let's start the game!" << endl;
cout << endl;
cout << player1 << " please input the 5 letter word you want " << player2 << " to guess." << endl;
cin >> word;
//Round 1
if (word.length() == 5) // IF the length of the word IS EQUAL TO (==) 5:
{
word.at(0) = tolower(word.at(0)); //
word.at(1) = tolower(word.at(1)); //
word.at(2) = tolower(word.at(2)); /// IF word AT (.at) position (x) = tolower word.at(x) / if word at (x) is "A", tolower word.at(x) is "a"
word.at(3) = tolower(word.at(3)); //
word.at(4) = tolower(word.at(4)); //
cout << "You have 5 guesses. Enter your first letter guess: " << endl; //User starts with 5 guesses. This is the first letter guess.
cin >> letterguess;
letterguess.at(0) = tolower(letterguess.at(0));
if (letterguess.length() != 1) // IF the letter guess IS NOT EQUAL TO (!=) 1: Then it is a bad input. Run the program again.
{
cout << "You are only supposed to input 1 letter. Run program again." << endl;
validLetterGuess = false;
}
if (letterguess.at(0) == word.at(0) && validLetterGuess == true)
{
cout << "You guessed the first letter right!" << endl;
dashes.at(0) = letterguess.at(0);
}
if (letterguess.at(0) == word.at(1) && validLetterGuess == true)
{
cout << "You guessed the second letter right!" << endl;
dashes.at(2) = letterguess.at(0);
}
if (letterguess.at(0) == word.at(2) && validLetterGuess == true) // Round 1
{
cout << "You guessesd the third letter right!" << endl;
dashes.at(4) = letterguess.at(0);
}
if (letterguess.at(0) == word.at(3) && validLetterGuess == true)
{
cout << "You guessed the fourth letter right!" << endl;
dashes.at(6) = letterguess.at(0);
}
if (letterguess.at(0) == word.at(4) && validLetterGuess == true)
{
cout << "You guessed the fifth letter right!" << endl;
dashes.at(8) = letterguess.at(0);
}
if (validLetterGuess == true)
{
cout << dashes << endl;
cout << "You have 5 guesses. Enter your first word guess : " << endl;
cin >> wordguess1;
}
}
if (wordguess1 == word)
{
GameOver = true;
cout << "You guessed the word correctly in ONE guess! YOU WIN!!" << endl;
}
|