lundi 30 janvier 2017

C++ Checking if a character is not in a string with || not working

I am working on this game where the player gets asked to play again. I am used to checking if a condition is not met and then return false, so at the end I can simply add return true. This also helps with nesting.

If I do it the other way around it works:

bool AskToPlayAgain() {
    cout << "Do you want to play again? ";
    string Response = "";
    getline(cin, Response);

    if (Response[0] == 'y' || Response[0] == 'Y')
    {   
        cout << "True!\n";
        return true;
    }   

    cout << "False!\n";
    return false;
}

This will return true on y and Y, and False on every other character.

However. The way I want it is like this:

if (Response[0] != 'y' || Response[0] != 'Y')
{   
    cout << "False!\n";
    return false;
}   

cout << "True!\n";
    return true;

Only, no matter the response, it will always validate to false. Why is that? And how can I fix it?

Aucun commentaire:

Enregistrer un commentaire