My Solution to Section02/Lecture23

So at the beginning of this lecture Ben creates a function called AskToPlayAgain explaining that he wants to be clear that the function prints a line asking for user input as well as processing and returning that input.

I just wanted to share my solution to this, putting the printed line at the end of PlayGame as that could be seen as part of playing the game. That is deciding whether or not to play again.

Sorry if this isn’t worthwhile or has already been discussed or something, just wanted to share.

//--------------------------//
int main()
{
printIntro();
playGame();
PlayAgain();
return 0;
}
//-------------------------------//
void playGame()
{
//run the game using a defined number of turns

constexpr int TURN_NUMBER = 5;

for (int i = 1; i <= TURN_NUMBER; i++) {
	string Guess = getGuess();
	cout << endl;
	cout << "Your guess was : " << Guess << "\n";
};

cout << "Would you like to play again?\n";

};

//-----------------------------------------//
bool PlayAgain()
{
string response = “”;
getline(cin, response);
return (response[0] == ‘y’) || (response[0] == ‘Y’);
};

Privacy & Terms