Calendar - display name of specific date

So basically I need to make the program display the 10 input dates then calculate the day of the week. The program runs and I believe the algorithm and most of the functions are correct but I am having trouble getting it to display the name of the day after calculated.
For example;
"1/10/1900 - Wednesday"

I am sure its something fairly simple and I just can't see but any help would be 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
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
  //Dekota Joe

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool leap(int);

class dateType
{
public:
	dateType();
	dateType(int, int, int);
	void setDate(int, int, int);
	void printDate(ostream&)const;

	friend ostream& operator <<(ostream &os, const dateType &d);
	friend istream& operator >>(istream &is, dateType &d);

protected:
	int month;
	int day;
	int year;

};

class nameDate : public dateType
{
public:
	nameDate();
	nameDate(int, int, int);
	nameDate(const nameDate&);
	void setDate(int, int, int);
	void setName();
	friend ostream &operator <<(ostream &, const nameDate &);
	friend istream &operator >>(istream &, const nameDate &);

protected:
	string name;
	void setName(string);

};

ostream& operator<<(ostream &os, const dateType &d) { 
	os << d.month << "/" << d.day << "/" << d.year;

	return os;
}


istream& operator >>(istream &is, dateType &d) {
	is >> d.month >> d.day >> d.year;

	return is;
}


ostream& operator<<(ostream &outFile, const nameDate &d)
{
	outFile << d.month << "/" << d.day << "/" << d.year << " - " << d.name;

	return outFile;
}

int main() {
		dateType d1;
		cout << "Testing default constructor..." << endl;
		d1.printDate(cout);

		cout << "\nTesting preset constructor..." << endl;
		dateType d2(9, 13, 2000);
		d2.printDate(cout);

		ofstream outFile;
		outFile.open("dateProgram.txt");

		if (outFile.fail()) {
			cout << "\n\nFailed to open \'dateProgram.txt\'\n" << endl;

			system("pause");
			return 0;
		}

		outFile << "Dekota Brown" << endl << endl;

		outFile << "\nTesting default constructor..." << endl;
		d1.printDate(outFile);

		outFile << "\nTesting preset constructor..." << endl;
		d2.printDate(outFile);

		cout << "\nTesting setDate..." << endl;
		outFile << "\nTesting setDate..." << endl;
		d1.setDate(3, 4, 1997);
		d1.printDate(cout);
		d1.printDate(outFile);

		cout << "\nTesting overloaded insertion operator..." << endl;
		dateType d(9, 11, 2001);
		cout << d;
		outFile << "\nTesting overloaded insertion operator..." << endl;
		outFile << d;

		ifstream inFile;
		inFile.open("inputDates.txt");
		
		if (inFile.fail()) {
			cout << "\n\nFailed to open \'inputDates.txt\'\n" << endl;

			system("pause");
			return 0;
		}
		
		cout << endl << endl;
		outFile << endl << endl;

		cout << "Testing name of date constructor..." << endl;
		outFile << "Testing name of date constructor..." << endl;

		cout << endl;
		
		for (int i = 0; i < 10; i++) {
			inFile >> d1;
			nameDate name;
			outFile << d1 << endl;
			cout << d1 << "\t" << name << endl;
		}

		outFile.close();
		inFile.close();

		cout << endl << endl;
		system("pause");
		
		return 0;
	}

	dateType::dateType() {
		month = 1;
		day = 1;
		year = 0000;
	}

	dateType::dateType(int myMonth, int myDay, int myYear) {
		month = myMonth;
		day = myDay;
		year = myYear;
	}

	void dateType::setDate(int myMonth, int myDay, int myYear) {
		month = myMonth;
		day = myDay;
		year = myYear;
	}

	void dateType::printDate(ostream& out)const {
		out << month << "/" << day << "/" << year << endl;
	}
	
	bool leap(int year)
	{
		return(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}

	void nameDate::setName() {

		int total;

		total = year + year / 4 - year / 100 + year / 400 + 1;

		switch (month) {

		case 1:
		case 10:
			break;

		case 2:
		case 3:
		case 11: total = total + 3;
			break;

		case 5: total = total + 1;
			break;

		case 8: total = total + 2;
			break;

		case 6: total = total + 4;
			break;

		case 9:
		case 12: total = total + 5;
			break;

		case 7:
		case 4: total = total + 6;
			break;
		}

		if (month < 3 && leap(year))
			total--;

		total = total + (day - 1);

		total = total % 7;

		switch (total) {
		case 0: name = "Saturday";
			break;
		case 1: name = "Sunday";
			break;
		case 2: name = "Monday";
			break;
		case 3: name = "Tuesday";
			break;
		case 4: name = "Wednesday";
			break;
		case 5: name = "Thursday";
			break;
		case 6: name = "Friday";
			break;
		}
	}

void nameDate::setDate(int m, int d, int y) {
	month = m;
	day = d;
	year = y;
	setName();
}

nameDate::nameDate() {
	month = 1;
	day = 1;
	year = 0000;
	name = "Saturday";
}


inputDates.txt
1 10 1900
3 16 1900
2 2 1944
1 1 2004
2 29 2004
12 24 2002
7 2 2050
3 23 2004
1 6 1812
2 3 1800
On line 127 you create a nameDate object, but you never initialize it with the date you got from file.

I'm not sure, exactly, why you have a separate class for that; the dateType class should be able to tell you what day of week it is.

If you do keep it separate, you should probably make it possible to take a dateType class as argument to the constructor/setter of the nameDate class. And you probably ought to make some accessors to get the year, month, and day values out of a dateType.

For reference, the simplest algorithm to get the weekday in the Gregorian system is:

1
2
3
4
5
6
7
8
9
10
// http://www.faqs.org/faqs/calendars/faq/part1/index.html
unsigned day_of_week( unsigned year, unsigned month, unsigned day )
{
  unsigned a, y, m;
  a = (14 - month) / 12;
  y = year - a;
  m = month + (12 * a) - 2;
  // Gregorian:
  return (day + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12)) % 7;
}

You can use that as an index into an array listing the weekday names:

1
2
  std::string weekday_names[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  std::string weekday_name = weekday_names[ d1.dayOfWeek() ];

Hope this helps.
Topic archived. No new replies allowed.