Briefing: I have put a check of users valid input if he wants to play again or not. On selecting Y, the game restarts. On selecting N the game ends. But on selecting W (any other option) the game asks again for valid selection from Y or N. But this time if user selects N the game restarts. Where as the game should have ended.
Bug regeneration steps:
- Start the Game
- Enter abcd and press enter (do this 5 times in total)
- Enter W press enter (Game will ask again for valid input from Y or N)
- Enter N [bug: Logical error. The game should have ended but rather it starts again.]
Code snippet:
bool AskToPlayAgain(); //Function prototype
//Entry point
int main()
{
do {
PrintIntro();
PlayGame();
} while (AskToPlayAgain()); //AskToPlayAgain() returning true where it should return false as per the steps
return 0;
}
bool AskToPlayAgain() //Method definition
{
std::string playAgain = “”;
std::cout << "Do you want to play again? [Y/N]: ";
std::getline(std::cin,playAgain);
if (playAgain[0] == ‘y’ || playAgain[0] == ‘Y’)
{
return true;
}
else if (playAgain[0] == ‘n’ || playAgain[0] == ‘N’)
{
return false;
}
else
{
std::cout << “\nWrong input. Please enter again\n.”;
AskToPlayAgain();
}
}
Project Link:
BullCowGame.zip (241.6 KB)
Please let me know why is it happening?