c++ 程序应该在我输入日期的第二天告诉我

q35jwt9p  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(132)

它通常工作,但在4,6,9和11个月的任何特定日期,它只是跳到下一个月的第一天。

#include <iostream>
using namespace std;

int main()
{
    cout << "What is the day after the date you enter?:";
        int day, month, year;
    cin >> day >> month >> year;
    if (day == 31 && month == 12)
    {
        day = 1;
        month = 1;
        year = year + 1;
    }
    else if (day == 31 && month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)
    {
        day = 1;
        month = month + 1;
    }
    else if (day == 30 && month == 4 || month == 6 || month == 9 || month == 11)
    {
        day = 1;
        month = month + 1;
    }
    else if (day == 28 && month == 2)
    {
        day = 1;
        month = month + 1;
    }
    else if (day == 29 && month == 2)
    {
        day = 1;
        month = month + 1;
    }
    else
    {
        day = day + 1;
    }
    cout << "The day after the date you entered is: " << day << "/" << month << "/" << year << endl;
    return 0;
    system("pause");
}

`
例如,我使用的第一个日期是14 09 2020,它输出1/10/2020
this is a picture for proof
Idk怎么办,请帮助

k7fdbhmy

k7fdbhmy1#

你需要更多的支架

else if (day == 31 && month == 1 || month == 3 || month == 5 ||
    month == 7 || month == 8 || month == 10)

应该是

else if (day == 31 && (month == 1 || month == 3 || month == 5 || 
    month == 7 || month == 8 || month == 10))

在下一个else if上也出现相同的错误。
&&的优先级高于||,因此您需要使用方括号来解决此问题。

相关问题