全是合理 2021-05-09 15:03 采纳率: 100%
浏览 2414
已采纳

c++一直提示未定义标识符是怎么回事啊

  • 写回答

5条回答 默认 最新

  • benbenli 2021-05-10 05:33
    关注

    你的变量 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.
    
    
    

    如果你满意我的回答,请点采纳,我很感谢你的认可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突