Return true or false? Why doesn't this work?

After asking the player whether they wish to play again, the asking function should return TRUE if either one of the following is true: First character of Response is ‘y’ or “Y”.

Why then doesn’t this work?

bool AskToPlayAgain()
{
    cout << "Do you want to play again?\n";
    string Response = "";
    getline(cin, Response);
    
    return ((Response[0]=='y')||(Response[0]=='Y'));
}

Specifically the return line at the end. Shouldn’t it return true if at least one of the nested conditionals is also true?

Thanks for any help!
-Ben

Looks fine to me. The problem might be in the caller function where you check the return value of AskToPlayAgain

I tested by myself and I see no problems with it. Maybe you should show your code here to check somewhere are wrong.

The code looks right but the outer parenthesis is not needed. I think you were expecting something to show on your screen but you don’t have it set that way. In other words, the program is spitting out true when ‘y’ or ‘Y’ is entered but you can’t see cause it’s running in the background. If you want to check you can do what Ben did in the video and put cout << in front of the boolean function. Hope this helps.

Privacy & Terms