mardi 11 février 2020

Why does C++ not read the next line of my if-else statement although the prior statement should not be true? [duplicate]

int main()

{

int month, year;
cout << "Enter a month (1-12) and year (1-2100): ";
cin >> month >> year;


if ((year > 2100 || year < 1) || (month > 12 || month < 1))
{
    cout << "Invalid Date";
}
else
{
     if ((month == 2) && (year % 100 == 0 && year % 400 == 0))
    {
        cout << "This month has 29 days";
    }
    else if ((month == 2) && (year % 100 != 0) && (year % 4 == 0))
    {
        cout << "This month has 29 days";
    }
    else if (month == 2)
    {
        cout << "This month has 28 days";
    }
    else if (month == 1, 3, 5, 7, 8, 10, 12)
    {
        cout << "This month has 31 days";
    }
    else if (month == 4, 6, 9, 11)
    {
        cout << "This month has 30 days";
    }
}


return 0;

}

I am trying to tell the user how many days are in the month that they have entered. The first if and else-if in the nested else is meant to determine whether or not the year the user entered is a leap year, if it is, and the month is 2, then the code will display 29 days. This seems to be working ok.

My problem is when I try to enter the months of 4, 6, 9, 11. aka the months with 30 days. The result i 31 days. It seems to not be reading the last else-if statement, but I don't understand why it wouldn't.

Aucun commentaire:

Enregistrer un commentaire