Search code examples
c++if-statementcomparison-operators

If statements with multiple variables


I'm trying to use if statement with multiple comparison operations but the day variable isn't working in my if statements.

Here's my code:

int day;
string rain;

cout << "What day of the week is it?" << endl;
cin >> day;

while (0 < day < 8)
{
    cout << "Is it raining? Please type 'yes' or 'no' " << endl;
    cin >> rain;

    if ((0 < day < 3) && (rain == "yes"))
    cout << "Read in bed" << endl;

    else if ((0 < day < 3) && (rain == "no"))
        cout << "Go out and play!" << endl;

    else if ((2 < day < 8) && (rain == "yes"))
        cout << "Take an umbrella!" << endl;
    else
        cout << "No umberella needed" << endl;

    cout << "What day of the week is it?" << endl;
    cin >> day;
}

cout << "Invalid day sorry" << endl;

Getting Read in bed or go out and play, but never Take an umbrella.

My code also works if I put day = 9.


Solution

  • This has nothing to do with if statements and multiple variables, your 0 < day < 3 should actually read as 0 < day && day < 3. BTW, you don't need to test it in every branch of the same if statement, it is unlikely to change.