gpt4 book ai didi

c++ - 从指定日期起第二天显示的代码

转载 作者:搜寻专家 更新时间:2023-10-31 02:17:36 25 4
gpt4 key购买 nike

我一直在尝试编写一些代码,让我从用户输入的日期开始计算第二天的日期。这是我的代码。

#include <iostream>
#include <string>

using namespace std;

int daysMonth(int iMonth)
{
if (iMonth >= 1 && iMonth <= 12)
{
if (iMonth == 2)
return 28;

if (iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11)
return 30;

return 31;
}
return 0;
}

void nextDay (int iD, int iMonth, int iY)
{
int daysM = daysMonth(iMonth);

if (iD != daysM)
{
iD = iD + 1;
iMonth = iMonth;
iY = iY;
}

if (iD == daysM)
{
iD = 1;
iMonth = iMonth + 1;
iY = iY;
}

if ((iMonth == 12) && (iD == 31));
{
iD = 1;
iMonth = 1;
iY = iY + 1;
}

cout << iD << "-" << iMonth << "-" << iY << endl;
}

int main ()
{
int iDay, iMonth, iYear;

cout << "Input day" << endl;
cin >> iDay;

cout << "Input Month" << endl;
cin >> iMonth;

cout << "Input Year" << endl;
cin >> iYear;

if (iDay >= 1 && iDay <= 31 && iMonth >= 1 && iMonth <= 12)
{
nextDay(iDay, iMonth, iYear);
}
else
cout << "Invalid date." << endl;
}

我的问题是无论日期如何,每个日期都只会增加一年。我想知道它是否是 nextDay 函数的条件。任何帮助将不胜感激!谢谢!~

最佳答案

if ((iMonth == 12) && (iD == 31));
// ^^^

行尾的分号使 if 语句的主体为空,并且始终执行以下 block 。

关于c++ - 从指定日期起第二天显示的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35572414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com