Date Class Questions

Hello! I'm doing a bit of homework working with classes. The goal is to create a Date class that store a calendar date. I'm having a bit of trouble writing the member functions for the class specifically Show(), SetFormat(), and Compare(). The requirements for the member functions are as follows. Show( ): output date to screen; there is more than one possible format and the class needs to store the format setting. Show should use format setting to determine output. Should start in "default"; possible formats should include: 1. M/D/Y 2. mm/dd/yy 3. month D, Y (ex. Oct 13, 2014).
I'd like to know what it means by "store a format setting" and how to accomplish this. Where do I store it? Another function? SetFormat(): allows to change format setting. For this I would like to know how write it so that when a letter is entered it will display in format until changed again. Lastly, Compare(): function should compare two Date objects (the calling object and the parameter), and should return: -1 if the calling object comes first chronologically, 0 if the objects are the same date, and 1 if the parameter object comes first chronologically. I just don't know how to code it to compare two objects. It's a work in progress so I apologize for the mess. I included a commented out block at the end for when I was trying to work on the Setformat() function. I think I was on to something. Any and all help is appreciated!
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
  //------------------DATE.CPP-------------------------
//class definition for dates
//

#include <iostream>
#include "date.h"

using namespace std;

Date::Date(int month, int day, int year)
{
	if (true)
		Input();
	else
		Date();
}

Date::Date()
{
	month = 1;
	day = 1;
	year = 2000;

}

void Date::Input()
{
	cout << "Please input a date!";
	cin >> month;
	cin >> "/";
	cin >> day;
	cin >> "/";
	cin >> year;
	//should this be entered as a string? 

	if((month < 1) || (month > 12))
	{
		cout << "Invalid entry." << endl;
	}
	if ((day < 1) || (day > 31))
	{
		cout << "Invalid entry." << endl;
	}
	if (year < 0)
	{
		cout << "Invalid entry." << endl;
	}

}

void Date::show()
{
	cout << month << '/' << day << '/' << year;
	// do I pass in SetFormat()? how?

}

bool Date::Set(int m, int d, int y)
{
	if (true)
	{
		if (month >= 1 && month <= 12)
		{
			cin >> m;
			month = m;
		}
		if (day >= 1 && day <= 31)
		{
			cin >> d;
			day = d;
		}
		if (year >= 0)
		{
			cin >> y; 
			year = y;
		}
	}
	else
	{
		abort;
	}
}

int Date::GetMonth()
{
	return month;
}

int Date::GetDay()
{
	return day;
}

int Date::GetYear()
{
	return year;
}

bool Date::SetFormat(char f)
{
	cin >> f;

	switch(f)
	{
		case 1:
			f = 'D';
			cout << "Default format selected! ";
			break;
		case 2:
			f = 'T';
			cout << "Two-Digit format selected! ";
			break;
		case 3:
			f = 'L';
			cout << "Long format selected! ";
			break;
		default:
			f = 'D';
			break;
	}
}

void Date::Increment(int numDays = 1)
{

}

int Date::Compare(const Date& d)
{

}

/*
switch(int month)
{
	case 1:
		if(month == 1 || 01);
		cout << "Jan";
		break;
	case 2:
		if(month == 2 || 02);
		cout << "Feb";
		break;
	case 3:
		if(month == 3 || 03);
		cout << "Mar";
		break;
	case 4:
		if(month == 4 || 04);
		cout << "Apr";
		break;
	case 5:
		if(month == 5 || 05);
		cout << "May";
		break;
	case 6:
		if(month == 6 || 06);
		cout << "June";
		break;
	case 7:
		if(month == 7 || 07);
		cout << "July";
		break;
	case 8:
		if(month == 8 || 08);
		cout << "Aug";
		break;
	case 9:
		if(month == 9 || 09);
		cout << "Sept";
		break;
	case 10:
		if(month == 10);
		cout << "Oct";
		break;
	case 11:
		if(month == 11);
		cout << "Nov";
		break;
	case 12:
		if(month == 12);
		cout << "Dec";
		break;
}
*/

Last edited on
should this be entered as a string?
No, it's ok that way.

do I pass in SetFormat()? how?
No, but you should take f into account when printing the date. f is supposed to be a member of the class.

I just don't know how to code it to compare two objects.
First compare year. If the is not equal you found the answer already. If it is equaly compare month...
1
2
3
4
5
6
7
8
if(d.year < year)
  return -1;
else if(d.year > year)
  return 1;
else if(d.month < month)
  ...

return 0;
Topic archived. No new replies allowed.