increment date...

Im have been trying to figure for the last couple of days a way to add a certain number of days to a set date and make that the new set date. Here is what I a have so far:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>

#include "dateType.h"

using namespace std;

void dateType::setDate(int month, int day, int year)
{
    if(month < 1 || month > 12)
    {
        dMonth = 1;
        cout << "Invalid month set. Defaulted to 1." << endl;
    }
    else
        dMonth = month;
    
    dYear = year;
    
    switch(dMonth)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if(day < 1 || day > 31)
            {
                dDay = 1;
                cout << "Invalid day set. Defaulted to 1." << endl;
            }
            else
                dDay = day;
            break;
    }
    switch(dMonth)
    {
        case 2:
            if(dateType::isLeapYear())
            {
                if(day < 1 || day > 29)
                {
                    dDay = 1;
                    cout << "Invalid day set. Defaulted to 1." << endl;
                }
                else
                    dDay = day;
            }
            else
            {
                if(day < 1 || day > 28)
                {
                    dDay = 1;
                    cout << "Invalid day set. Defaulted to 1." << endl;
                }
                else
                    dDay = day; 
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            if(day < 1 || day > 30)
            {
                dDay = 1;
                cout << "Invalid day set. Defaulted to 1." << endl;
            }
            else
                dDay = day; 
            break;        
    }
}
void dateType::setMonth(int month)
{
    dMonth = month;
}
void dateType::setDay(int day)
{
    dDay = day;
}
void dateType::setYear(int year)
{
    dYear = year;
}
int dateType::getDay()
{
    return dDay;
}
int dateType::getMonth()
{
    return dMonth;
}
int dateType::getYear()
{
    return dYear;
}
int dateType::getDaysInMonth()
{
    switch(dMonth)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            dDaysInMonth = 31;
    }
    switch(dMonth)
    {
        case 2:
            if(dateType::isLeapYear())
            {
                dDaysInMonth = 29;
            }
            else
            {
                dDaysInMonth = 28;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            dDaysInMonth = 30;
            break;        
    }
    return dDaysInMonth;
}
int dateType::numberOfDaysPassed()
{
    int count = dMonth;
    int total = 0;
    
    dMonth = 0;
    
    for(int i = 0; i < count; i++)
    {
        dMonth++;
        total += dateType::getDaysInMonth();
    }
    return total - (dateType::getDaysInMonth() - dDay);
}
int dateType::numberOfDaysLeft()
{
    int total = 0;
    if(dateType::isLeapYear())
        total = 366 - dateType::numberOfDaysPassed();
    else
        total = 365 - dateType::numberOfDaysPassed();
    return total;   
}
void dateType::incrementDate(int num)
{

}
void dateType::printDate()
{
    cout << dMonth << "-" << dDay << "-" << dYear;
}
bool dateType::isLeapYear()
{
    if(dYear % 4 == 0)
        return true;
    else
        return false;
}
dateType::dateType(int month, int day, int year)   
{
    if(month < 1 || month > 12)
    {
        dMonth = 1;
        cout << "Invalid month set. Defaulted to 1." << endl;
    }
    else
        dMonth = month;
    
    dYear = year;
    
    switch(dMonth)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if(day < 1 || day > 31)
            {
                dDay = 1;
                cout << "Invalid day set. Defaulted to 1." << endl;
            }
            else
                dDay = day;
            break;
    }
    switch(dMonth)
    {
        case 2:
            if(dateType::isLeapYear())
            {
                if(day < 1 || day > 29)
                {
                    dDay = 1;
                    cout << "Invalid day set. Defaulted to 1." << endl;
                }
                else
                    dDay = day;
            }
            else
            {
                if(day < 1 || day > 28)
                {
                    dDay = 1;
                    cout << "Invalid day set. Defaulted to 1." << endl;
                }
                else
                    dDay = day; 
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            if(day < 1 || day > 30)
            {
                dDay = 1;
                cout << "Invalid day set. Defaulted to 1." << endl;
            }
            else
                dDay = day; 
            break;        
    }
}


The function incrementDate (the empty function) has been killing me. I have tried multiple things including the following:

1
2
3
4
5
6
7
8
9
10
void dateType::incrementDate(int num)
{
    num -= (dateType::getDaysInMonth() - dDay);
    do
    {
        dMonth++;
    }
    while(num > dateType::getDaysInMonth());
    dDay = num;
}


It doesn't take a genius to figure out the above will only work for so many days before it starts incrementing the months too and not adjusting the days...does anyone have guidance for me??
What you could do is:

1. Check if adding "num" will overflow the current month. If not, you're done; you just have to add "num" to the current day.
2. If yes, figure out how many days until the next month and subtract that from "num". Now, set the day to the 1st and set the month to the next month. Increment the year if it's now January. Go back to 1.

You could also implement your date internally as an unsigned int representing the days since January 1, year 0. That way, you just have to write a two-way conversion between that number and the month, day, and year. With this approach, incrementDate would be just adding num to the internal value.
I feel like a huge moron after at least 3 to 4 days of trying to figure this out I finally have it with the small bit of code below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void dateType::incrementDate(int num)
{
    while ((num + dDay) > dateType::getDaysInMonth())
    {
        num -= dateType::getDaysInMonth();
        if (dMonth == 12)
        {
            dMonth = 0;
            dYear++;
        }   
        dMonth++;
    }
    dDay += num;
}
Topic archived. No new replies allowed.