Hi guys,
I'm having some problem with my C++ assignment. Can't get this part down.

class Date
{
    private:
            static const int daysInMonth[13];
            int monthNo, year, day;
            string month;
            bool validDate, leapYear;
    
    public:
           void displayDaysInMonth();
           void displayDate();
           void enterDate();
           void verifyDate();
           void displayOptions();
           void ahqError()
                {cout << "Error! Enter only a-g or q\n";}; 
           void setDay(int);
           void setYear(int);
           void setMonth(string);
};
const int Date::daysInMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

Background of my assignment: It's a calender/date check program. Thus, I need it to spot a leap year, thus I need to edit the February month to 29 when necessary.

This is the class I defined for my assignment. It works fine until I need to edit the array values. I tried declaring a normal array, however it doesn't work.

Google'ing turned up the solution of using constant/static arrays.
However, I need to edit 1 of the values.
Any solutions to this? Thanks in advance.

Edit: I know it sounds stupid but I just noticed the constant thing hanging around. Came with the codes I google'ed up, now trying without them. But anyone with any ideas for declaring arrays in class is welcome. :D

What's a problem? Do not declare this static member array as const - that's all.

At first YOU say to compiler that "it's a constant array, don't allow ME to change it" - then cry ;)...

Sorry I don't get your meaning. I figure out my mistake after posting.
Now I'm figuring out why I'm unable to declare an array in class like in a struct.
Cause it seems weird for me to declare an array like that.

>>Any solutions to this?

On the other hand, depending how you want to do things, do you even want to change the array values? You could make the change only if needed in a function. Say something like this:

void Date::operator++() //prefix increment operator
{
   //.......
   if(monthNo == 2)
         if(day < 28)
           ++day;   
         else if (day == 28)
           if(leapYear)
            ++day;
           else
              day = 1;
              month = 3;
        else //if(day == 29)
           day = 1;
           month = 3;
    //.....
}

Thanks for the constructive input.
I started learning more about classes this semester so I'm still a greenhorn at this.
I can edit the array values after removing the constant. Silly me.
Is

        `static int daysInMonth[13];`

and

int Date::daysInMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

the only way to declare arrays in class? Only by using static variables?

Sorry I don't get your meaning. I figure out my mistake after posting.
Now I'm figuring out why I'm unable to declare an array in class like in a struct.
Cause it seems weird for me to declare an array like that.

Why you unable to declare an array in class like in a struct?!
May be you have your own personal C++ language?

Oh, I see: you can't INITIALIZE non-static array member in a class definition with initializer-list! Yes, it's impossible in modern C++.
In actual fact it's not a tragedy, it's discomfort (to some extent). Add initialization code in the class constructor, for example:

class CanDoThat
{
    int array[12];
public: CanDoThat() {
    static int data[12] = { 0,1,2,...11 };
    for (int i = 0; i < 12; ++i)
        array[i] = data[i];
    }

The class constructor initialize class objects. Moreover, really this (or like this) code works for all non-static regular arrays initialized by initializer-list...

Ok, thanks for the tip. Guess I better start getting used to using static for arrays.
Or until I learn more about vectors. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.