24. Booleans and Comaprisons

Hello everyone,

I have a question and I am thinking to complicated for this tho. But we are asked to write the validation for the AskToPlayAgain() function. When i saw the answer it thought it was a bit to simplified. I have some years of coding and scripting (mainly Powershell scripting) and i always learned myself to be clear with validation when it comes to the user input.

My Function

bool AskToPlayAgain() {

bool Answer;

cout << "Do you want to play again? (Yes or No): ";
string Response = “”;
getline(cin, Response);

if ((Response[0] == ‘y’) || (Response[0] == ‘Y’)) {

  cout << "You choose to run the game again!" << endl;
  Answer = true;

}
else if ((Response[0] == ‘n’) || (Response[0] == ‘N’)) {

  cout << "You choose not the play the game again." << endl;
  Answer = false;

}
else {

  cout << "You have to choose Yes or No" << endl;
  Answer = false;

}

return Answer;
}

Or is this a bit to advanced for this stage of the course?

Thanks!

Your code looks really good. I’m sure we are getting to that but he is merely introducing boolean to us in this video in that we are going to use it in the game but he doesn’t fully use it for the game as he is trying to let us newbies digest what boolean does.

Privacy & Terms