jeudi 2 mars 2017

In C++ how to use the or statement (||) correctly, in an else-if statement

I'm learning a new programming language (C++). I have past experience with a few dedicated languages and Python, so some things are carrying over for me. Unfortunately, one thing that I thought would carry over, doesn't seem to work. I'm trying to use the or statement correctly while being nested in an else-if statement. Below is the code I tried. (I'm only posting part of the code and replacing the code inside the if statements with simple out statements for testing purposes). By the way I'm using Visual Studio Community 2017 RC.

string GameExit;
bool GameChoiceGo = true;

while (GameChoiceGo == true)
{
    system("cls"); 
    cout << "\n  Are you sure you want to exit? (Yes or No)     ";
    cin >>  GameExit;
    if (GameExit == "y" || "Y" || "yes" || "Yes" || "YES")
    {
        cout << "User typed Yes";
        Sleep(3000);
        system("cls");
        break;
    }
    if (GameExit == "n" || "N" || "no" || "No" || "NO")
    {
        cout << "User typed No";
        Sleep(3000);
        system("cls");
        GameChoiceGo = false;
    }
    else
    {
        cout << "\nI'm sorry but, " << GameExit << " is not a acceptable choice. Type: Yes or No.\n\n\n";
        Sleep(3000);
        system("cls");
    }
}
break;

When the code is written this way, only the first statement is activated. Even if the user types "No" or anything else, it will output "user typed yes". The else-if statements work if I replace the or statements with only one statement (i.e. "y" and "n"). The only problem is, I want to have any possible version of yes and no that the user might type in the code. Please let me know why this is not working correctly. Thank you!

Aucun commentaire:

Enregistrer un commentaire