I don't understand the Booleans in this section

So I have this boolean that I just don’t understand

bool PlayAgain()
cout << "Do you want to play again? (y/n)? ";
string Response = “”;
getline(cin, Response);
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);

So this boolean is returning y or Y, how does this correlate to true or 1?


int main()
bool bPlayAgain = false;
bPlayAgain = PlayAgain();
} while (bPlayAgain);
return 0;

I create this boolean bPlayAgain. This is set to the output of the PlayAgain Function. So if I am not mistaken this would change the bool equals to ‘y’. How does this while loop work?

Is a boolean set to true automaticall?

Hi Donald,
New here myself but I found this information on control statements which may be helpful


Here is where someone asked a similar question in the cplusplus.com forums



@Donald_Hall As far as I know booleans are always false. If you think of it scientifically, something is always false, until proven (or in the case of programming given a condition to be true).

As for your first set of code, because it is a boolean it can only return a true or false, 1 or 0 respectively. When someone types in “y” or “Y” it sets the boolean to true, and resets the game.

Your 2nd set of code is associated with your first set. Essentially it is creating a boolean that is false, and playing the game until that boolean is set to true, to restart it. If it is set to false, by having a letter be in the first spot of “Response” that is not “y” or “Y”, then the game ends.

If someone has a better response or can clear up mine, I’d appreciate it and hope it helped atleast a litte.

It’s a conditional statement, it’s comparing if the first character of Response (Response[0]) is equal to ‘y’
Response[0] == 'y' OR if the first character is equal to ‘Y’. It’s not returning either ‘y’ or ‘Y’ but the result of the conditional statement.

For example if “no” was entered Response[0] would be ‘n’ so 'n' == 'y' would be false because they are not equal.

Privacy & Terms