trying to make a function that adds days to the current date

Aloha Everybody,

I've been reading a lot about classes and object-oriented programming recently and I'm attempting to define a class called date with multiple member functions. One of my functions is called addDay() and I want it to be able to add a specified number of days to the current day, and be accurate throughout the months with different # of days.

So far, I feel like I am close, but I think I need to have the entire function in a loop in order for my "if else if" statements to be accurate. The problem is, I can't think of a good way to nest all of my if statements in a loop that breaks at the proper time. I'm a very amateur programmer trying my best to learn, so if there is a more efficient way of doing this and you would like to share, feel free as I will study it and be greatly appreciative. Thanks in advance!

Here is what I have so far: (day and month are private data members)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void addDay(int n) {
        int newDay = day + n;
            if(month == 1 || month == 3 || month == 5
               || month == 7 || month == 8 || month == 10
               || month == 12){
                if (newDay > 31) {
                    while (newDay > 31 && month == 1 || month == 3
                           || month == 5 || month == 7 || month == 8
                           || month == 10 || month == 12) {
                        newDay -= 31;
                        month += 1;
                    }
                }
            day = newDay;
            }
            else if(month == 4 || month == 6 || month == 9
                    || month == 11) {
                if (newDay > 30) {
                    while (newDay > 30 && month == 4 || month == 6
                    || month == 9 || month == 11) {
                        newDay -= 30;
                        month += 1;
                    }
                }
            day = newDay;
            }
            else if(month == 2) {
                if (newDay > 28) {
                    while (newDay > 28 && month == 2) {
                        newDay -= 28;
                        month += 1;
                    }
                }
            day = newDay;
            }

            else
                day = n + day;

    }


The addDay function I am trying to build came from an addMonth function I wrote that worked really well. I thought I could just modify it and make the necessary changes to challenge myself. Below is the addMonth function I wrote so you can see how I was thinking about the addDay function.

1
2
3
4
5
6
7
8
9
10
    void addMonth(int n) {
        int newMonth = month + n;
            if (newMonth > 12) {
                while (newMonth > 12)
                    newMonth -= 12;
                month = newMonth;
            }
            else
                month = n + month;
    }
Last edited on
Hi there,

Why don't you use one simple array that holds the maximum numbers for every month, that way you can just do the same operation without having to to use a bunch of if/else's.

For example:

1
2
3
4
5
6
7
8
int month_max[12] = {31, 28,31,30,31, 30, 31,31, 30,31,30,31};

...

if (newDay > month_max[month-1])  // month-1 because arrays start at 0
{
    // add one to month
}


Also, I don't understand why you're usig a while loop to reset days or months to zero, if you do

1
2
while (newMonth > 12)
                    newMonth -= 12;


It will normally always run only once, or if month is really high will give unexpected results.

If you're adding 3 months to december, the number will be 15, if you just add one year and then set month to 15-12, it will solve the problem much more easily, and just as important, make your code more readable to others as it's more clear what you're doing.

Hope that helps.

All the best,
NwN
Thank you very much NwN.

I figured there was a much more straight forward way of accomplishing this. Using an array is far more readable, not to mention much more efficient, than all of my if-else if statements with while loops. From now on, I will use arrays in similar situations.

I appreciate you're help and your valuable advice!
Topic archived. No new replies allowed.