How the computer knows True or False from the final bool function in the lecture?

Hi

From this

bool AskToPlayAgain() {
cout << "Would you like to play again? " << endl;
string Response{ “” };
getline(cin, Response);
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);
}

How does the computer knows it’s returning a False or true statement?

Is bool defaulting to return true (1) if the first Response char is set to my response cases?

Hey @loudforest dunno if you have got a better understanding of this yet.

But from what i could make out:

The code is looking for the first letter in the response which you are setting to ‘y’ or ‘Y’.

If the input is yes etc it will see that ‘y’ is the first character and it will execute the class therefore setting the bool to true

However if the response is anything else it will set the bool to false and not execute the class.

You aren’t explicitly making the bool default to any value, but more allowing the user to set the bool letting the user choose to play or not play again.

Hope that helps somehow, its what I got from the lecture.

@Jordan_Ashton is correct.

The return statement returns a boolean that is equal to the value (Response[0] == ‘y’) || (Response[0] == ‘Y’), meaning that if the response begins with the character “Y” it will return true, and false if it is anything else.

It is essentially shorthand for a more conventional statement like:

if (Response[0] == ‘y’) || (Response[0] == ‘Y’)
{
   return true;
}
else 
{
  return false;
}
1 Like

Privacy & Terms