std::string GetGuess(int GuessCount)
{
std::string guess = “”;
std::cout << “Enter guess #” << GuessCount << " of " << GUESS_LIMIT <<": ";
getline(std::cin, guess);
std::cout << std::endl;
return guess;
}
void PlayGame()
{
std::cout << “You have " << GUESS_LIMIT << " guesses.” << std::endl;
std::cout << std::endl;
for (int i = 1; i < GUESS_LIMIT + 1; i++)
{
std::string guess = GetGuess(i);
std::cout << "Guess " << i << " of " << GUESS_LIMIT << " was: " << guess << std::endl;
std::cout << std::endl;
}
if (AskToPlayAgain()) {
std::cin.get(); // Is this the best way?
PlayGame();
}
}
bool AskToPlayAgain()
{
std::cout << “Would you like to play again? (y/n)” << std::endl;
char answer = ::getchar();
std::cout << std::endl;
return answer == ‘y’ || answer == ‘Y’;
}
Sample Output:
Enter guess #10 of 10: test 10
Guess 10 of 10 was: test 10
Would you like to play again? (y/n)
y
You have 10 guesses.
Enter guess #1 of 10:
Guess 1 of 10 was:
Enter guess #2 of 10:
^This was without the line : std::cin.get();
At the end of the PlayGame() method, I was having a problem if the user wanted to start a new game. After entering ‘y’ or ‘Y’, The first guess would be prepopulated with data that came from the last game loop. I managed to temporarily fix the issue by the line std::cin.get(); and I was wondering if there was a better way of accomplishing this. Thanks