你的变量 a 是定义在同一scope,但是你定义了2次,编译器好像还每理解你的意图 (可能因为加了括号的缘故),所以它说还每定义。这样写吧,运行通过了。其实还要加上输入错误检测,年份范围,月份1-12, 天也要在每月天数范围等。
#include <iostream>
using namespace std;
int main()
{
int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap_month_days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
cout << "please enter year, month and day, and press ENTER:";
int y, m, d;
cin >> y >> m >> d;
int* md = (y % 4 == 8 && y % 100 != 0 || y % 400 == 0) ? leap_month_days : month_days;
int s = d;
for (int i = 0; i < m - 1; ++i)
{
s += md[i];
}
cout << "It's the " << s << "th day in the year." << endl;
return 0;
}
// Output
please enter year, month and day, and press ENTER:2021 5 9
It's the 129th day in the year.
please enter year, month and day, and press ENTER:2021 01 30
It's the 30th day in the year.
please enter year, month and day, and press ENTER:2020 3 1
It's the 60th day in the year.
如果你满意我的回答,请点采纳,我很感谢你的认可。
你的 a[] 定义的都是局部变量,所以在外层无法访问。你可以定义两个数组,一个表示平年,一个表示闰年,在 while 里面判断加哪个数组的值
你定义的a数组是一个局部变量,所以后面访问不到,把a数组的定义放在第5行后面,里面赋值就正常了。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y