C ++ Visual studio中的逻辑错误。数组是打印地址而不是内容

时间:2018-04-08 05:01:20

标签: c++ arrays

我在C ++ Visual Studio中遇到问题。我已经制作了一个数组MonthDays来存储它里面的月份日期。在使用DaysOfTheYear的构造函数中,我有一个字符串月份将月份设置为整数,将整数放在checker变量中。使用该检查器变量我正在尝试打印数组的内容,而是数组打印到我认为是变量的地址。下面是代码。在第79行,我有它在void print函数内打印数组的内容,但当然是打印出地址。我还没有在第79行编写代码,如果我输入60那么它将等于3月1日。我只是测试那部分以确保我在阵列中打印出正确的元素。在第13行,我在DayOfYear类中使用12个元素声明MonthDays [12]。在第136行,我使用月中的所有日期定义了MonthDays数组。我也知道,与第一个构造函数一样,它工作正常但是我应该将一个字符串变量和一个整数变量添加到一个新的构造函数中,该构造函数可以确定该条件下的日月格式的日期。

我的输出如下:

-8589934060(这应该是数组的内容)第365天是12月31日。 如果有人可以提供帮助,我将不胜感激。谢谢大家阅读本文。

 // This program takes a user inputted integer and converts it to a month and day

 #include <iostream>
 #include <string>
 #include <iomanip>
 #include <Windows.h>

 using namespace std;

 class DayOfYear
 {
     public:
         int MonthDays[12];
         int day;
         static string monthName;
         int checker;

         DayOfYear(int dayD)
         {
             day = dayD;
         }
         DayOfYear(int days, string month)
         {
             day = days;
             if (month == "January")
             {
                checker = 1;
             }
             else if (month == "February")
             {
                 checker = 2;
             }
             else if (month == "March")
             {
                checker = 3;
             }  
             else if (month == "April")
             {
                 checker = 4;
             }
             else if (month == "May")
             {
                 checker = 5;
             }
             else if (month == "June")
             {
                 checker = 6;
             }
             else if (month == "July") 
             {
                 checker = 7;
             }
             else if (month == "August")
             {
                 checker = 8;
             }
             else if (month== "September")
             {
                 checker = 9;
             }
             else if (month == "October")
             {
                 checker = 10;
             }
             else if (month == "November")
             {
                checker = 11;
             }
             else if (month == "December")
             {
                checker = 12;
             }

         }


     void print()
     {
         cout << MonthDays[checker - 1];

         if (day >= 1 && day <= 31)
         {
             cout << "Day " << day << " would be January " << day;
         }
         else if (day >= 32 && day <= 59)
         {
             cout << "Day " << day << " would be February " << day - 31;
         }
         else if (day >= 60 && day <= 90)
         {
             cout << "Day " << day << " would be March " << day - 59;
         }
         else if (day >= 91 && day <= 120)
         {
             cout << "Day " << day << " would be April " << day - 90;
         }
         else if (day >= 121 && day <= 151)
         {
             cout << "Day " << day << " would be May " << day - 120;
         }
         else if (day >= 152 && day <= 181)
         {
             cout << "Day " << day << " would be June " << day - 151;
         }
         else if (day >= 182 && day <= 212)
         {
             cout << "Day " << day << " would be July " << day - 181;
         }
         else if (day >= 213 && day <= 243)
         {
             cout << "Day " << day << " would be August " << day - 212;
         }
         else if (day >= 244 && day <= 273)
         {
             cout << "Day " << day << " would be September " << day - 243;
         }
         else if (day >= 274 && day <= 304)
         {
             cout << "Day " << day << " would be October " << day - 273;
         }
         else if (day >= 305 && day <= 334)
         {
             cout << "Day " << day << " would be November " << day - 304;
         }
         else if (day >= 335 && day <= 365)
         {
             cout << "Day " << day << " would be December " << day - 334;
         }
     }

 };


 int main()
 {
     int MonthDays[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

     DayOfYear tester(365,"February");
     tester.print();

     Sleep(5000);

 }

1 个答案:

答案 0 :(得分:2)

变化:

 int MonthDays[12];

要:

constexpr static int MonthDays[12] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 } ;

这设置了类的数组。

还要注意该行

cout << MonthDays[checker - 1];

访问班级MonthDays,而不是您在main

中撰写的班级