Hi,
I must be missing something really obvious but from what I can see, my code is quite similar to Ben’s (not identical) and it asks to play again twice, not once, when I run it in the console.
My code is as below. Apologies for not using code formatting, I don’t see any options in the text box.
#include <iostream>
#include <string>
using namespace std;
void PrintIntro();
void PlayGame();
string GetGuess();
bool AskToPlayAgain();
int main()
{
do
{
PrintIntro();
PlayGame();
AskToPlayAgain();
//cout << AskToPlayAgain();
} while (AskToPlayAgain() == true);
return 0;
}
void PrintIntro()
{
// introduce the game
constexpr int WORD_LENGTH = 9;
cout << "Welcome to Bulls and Cows, an amazing word game\n";
cout << "Can you guess the " << WORD_LENGTH;
cout << " letter isogram I am thinking of?\n";
cout << endl;
return;
}
void PlayGame()
{
constexpr int NUMBER_OF_TURNS = 5;
for (int i = 1; i <= NUMBER_OF_TURNS; i++)
{
//cout << "Enter Guess " << NUMBER_OF_TURNS;
cout << endl;
string Guess = "";
Guess = GetGuess();
cout << "Player guessed " << Guess << endl;
cout << endl;
}
}
// get a guess from player
string GetGuess() {
cout << "Enter your guess: ";
string Guess = "";
getline(cin, Guess);
//cin >> Guess;
return Guess;
}
bool AskToPlayAgain()
{
cout << "Do you want to play again?";
string Response = "";
getline(cin, Response);
return (Response[0] == 'y') || (Response[0] =='Y');
}