dateType class

Hello all. I am writing the definition of a class called dateType that stores, well, a date. The member variables of this class hold the month, day, and year while some operations on this data are setting the date, printing the date, etc. However, I have come across some trouble writing the definition of the function calculateNewDate as I simply have no clue how to go about writing it. Anyway, according to my book, this function is supposed to:
"Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3-19-2009 and the days to be added are 25, the new date is 4-12-2009."
Any help is appreciated.

The function calculateNewDate starts at line 150.
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <iostream>

using namespace std;

class dateType
{
public:
	void setDate(int month, int day, int year); 
	//set member variables dMonth, dDay, and dYear

	void setMonth(int month);
	//set member variable dMonth

	void setDay(int day); 
	//set member variable dDay

	void setYear(int year); 
	//set member variable dYear

	void calculateNewDate(int dayAmount);
	//calculate new date by adding a fixed amount of days to the date

	int getDaysInMonth() const;
	//return number of days in dMonth

	int getDaysPassed() const;
	//return number of days passed in the year

	int getDaysRemaining() const;
	//return number of days remaining in the year

	int getDay() const;
	//return dDay

	int getMonth() const;
	//return dMonth

	int getYear() const;
	//return dYear

	bool isLeapYear() const;
	//check if dYear is a leap year

	void printDate() const;
	//print the date in the format: dMonth-dDay-dYear

	dateType(int = 1, int = 1, int = 1900);
	//default constructor
private:
	int dMonth;
	int dDay;
	int dYear;
};

int main()
{	
	//Tested and verified all the functions, except calculateNewDate
	dateType myDate(12, 31, 2008);

	myDate.printDate();

	cout << "Total days passed in year is " << myDate.getDaysPassed() << endl;
	cout << "Total days remaining in year is " << myDate.getDaysRemaining() << endl;

	myDate.printDate();

	return 0;
}

void dateType::setDate(int month, int day, int year)
{
	if(1 <= month && month <= 12) //make sure value of dMonth is valid
		dMonth = month;
	else
		dMonth = 1;

	if(year > 0)//make sure value of dYear is valid
		dYear = year;
	else
		dYear = 1;

	if(dMonth <= 7) //Months in range Jan-July: Odd numbered months have 31 days, even months have 30
	{
		if(dMonth % 2 == 1 && (1 <= day && day <= 31)) //make sure dDay is valid
			dDay = day;
		else if((dMonth % 2 == 0 && dMonth != 2) && (1 <= day && day <= 30))
			dDay = day;
		else if((dMonth == 2 && isLeapYear()) && (1 <= day && day <= 29)) //During leap year, Feb. has 29 days
			dDay = day;
		else if((dMonth == 2 && !isLeapYear()) && (1 <= day && day <=28)) //During normal year, Feb. has 28 days
			dDay = day;
		else
			dDay = 1;
	}
	else //Months in range Aug.-December: Odd numbered months have 30 days, even months have 31 days
	{
		if(dMonth % 2 == 1 && (1 <= day && day <= 30))
			dDay = day;
		else if(dMonth % 2 == 0 && (1 <= day && day <= 31))
			dDay = day;
		else
			dDay = 1;
	}

}

void dateType::setMonth(int month)
{
	if(1 <= month && month <= 12) //make sure dMonth is valid
		dMonth = month;
	else
		dMonth = 1;
}

void dateType::setDay(int day)
{
	if(dMonth <= 7) //months in range Jan-July: Odd numbered months have 31 days, even months have 30
	{
		if(dMonth % 2 == 1 && (1 <= day && day <= 31))
			dDay = day;
		else if((dMonth % 2 == 0 && dMonth != 2) && (1 <= day && day <= 30))
			dDay = day;
		else if((dMonth == 2 && isLeapYear()) && (1 <= day && day <= 29)) //During leap year, Feb. has 29 days
			dDay = day;
		else if((dMonth == 2 && !isLeapYear()) && (1 <= day && day <=28)) //During normal year, Feb. has 28 days
			dDay = day;
		else
			dDay = 1;
	}
	else //months in range Aug-December: Odd numbered months have 30 days, even months have 31
	{
		if(dMonth % 2 == 1 && (1 <= day && day <= 30))
			dDay = day;
		else if(dMonth % 2 == 0 && (1 <= day && day <= 31))
			dDay = day;
		else
			dDay = 1;
	}
}

void dateType::setYear(int year)
{
	if(year > 0) //make sure year is valid
		dYear = year;
	else
		dYear = 1;

}

void dateType::calculateNewDate(int dayAmount)
{
	//stumped with this function
}

int dateType::getDaysInMonth() const
{
	if(dMonth <= 7) //find days of months in range Jan.-July
	{
		switch(dMonth % 2)
		{
		case 0:
			if(dMonth == 2 && isLeapYear())
				return 29;
			else if(dMonth == 2 && !isLeapYear())
				return 28;
			else
				return 30;
		case 1:
				return 31;
		}//end switch
	} //end if
	else //find months in range August-December
	{
		switch(dMonth % 2)
		{
		case 0:
			return 31;
		case 1:
			return 30;
		} //end switch
	} //end else

}

int dateType::getDaysPassed() const
{
	switch(dMonth)
	{
	case 1:
		return dDay;
	case 2:
		return (31 + dDay);
	case 3:
		if(isLeapYear())
			return (60 + dDay);
		else
			return (59 + dDay);
	case 4:
		if(isLeapYear())
			return (91 + dDay);
		else
			return (90 + dDay);
	case 5:
		if(isLeapYear())
			return (121 + dDay);
		else
			return (120 + dDay);
	case 6:
		if(isLeapYear())
			return (152 + dDay);
		else
			return (151 + dDay);
	case 7:
		if(isLeapYear())
			return (182 + dDay);
		else
			return (181 + dDay);
	case 8:
		if(isLeapYear())
			return (213 + dDay);
		else
			return (212 + dDay);
	case 9:
		if(isLeapYear())
			return (244 + dDay);
		else
			return (243 + dDay);
	case 10:
		if(isLeapYear())
			return (274 + dDay);
		else
			return (273 + dDay);
	case 11:
		if(isLeapYear())
			return (305 + dDay);
		else
			return (304 + dDay);
	case 12:
		if(isLeapYear())
			return (335 + dDay);
		else
			return (334 + dDay);
	}
}

int dateType::getDaysRemaining() const
{
	if(isLeapYear())
		return (366 - getDaysPassed());
	else
		return (365 - getDaysPassed());
}

int dateType::getDay() const
{
	return dDay;
}

int dateType::getMonth() const
{
	return dMonth;
}

int dateType::getYear() const
{
	return dYear;
}

bool dateType::isLeapYear() const
{
	return(dYear % 4 == 0 && (dYear % 100 != 0 || dYear % 400 == 0));
}

void dateType::printDate() const
{
	cout << dMonth << "-" << dDay << "-" << dYear << endl;
}

dateType::dateType(int month, int day, int year) //default constructor. Same body as setDate function
{
	if(1 <= month && month <= 12)
		dMonth = month;
	else
		dMonth = 1;

	if(year > 0)
		dYear = year;
	else
		dYear = 1;

	if(dMonth <= 7)
	{
		if(dMonth % 2 == 1 && (1 <= day && day <= 31))
			dDay = day;
		else if((dMonth % 2 == 0 && dMonth != 2) && (1 <= day && day <= 30))
			dDay = day;
		else if((dMonth == 2 && isLeapYear()) && (1 <= day && day <= 29))
			dDay = day;
		else if((dMonth == 2 && !isLeapYear()) && (1 <= day && day <=28))
			dDay = day;
		else
			dDay = 1;
	}
	else 
	{
		if(dMonth % 2 == 1 && (1 <= day && day <= 30))
			dDay = day;
		else if(dMonth % 2 == 0 && (1 <= day && day <= 31))
			dDay = day;
		else
			dDay = 1;
	}
}
Last edited on
The math is often done by converting to (then add days) and from Julian Day: http://en.wikipedia.org/wiki/Julian_day
Topic archived. No new replies allowed.