Calendar Reference

I found this code online while searching for a solution to something I wanted to do. I am wondering if it is possible to change this code to use it as a reference so that when a user were to input a a day, month, and year it would return what day of the week it is. Is it possible? Is there an easier way to accomplish what I'm talking about? Please help.

[\code]

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

bool isLeapYear(int); //good check for leap years
int firstDayofnewyearMonth(int);
int numOfDaysInMonth(int, bool); // takes the number of the month, a flag saying whether year is leap
void printHeader(int); // takes the number of the month, and the first day, prints, and updates
void printMonth(int, int&); // the first day of the next month
void skip(int);// prints the specified amount of spaces
void skipToDay(int);// prints leading spaces in monthly calendar
void disaster(); // terminates program in case of unrecoverable errors

int main()
{
system("color f1 ");
int year, firstDayInCurrentMonth;
int currentMonth = 1;
int numDays;
bool leap;
cout << "What year do you want a calendar for? ";
cin >> year;
cout << endl;
firstDayInCurrentMonth = firstDayofnewyearMonth(year);
leap = isLeapYear(year);
skip(9);
cout << year << endl;
while (currentMonth <= 12)
{
numDays = numOfDaysInMonth(currentMonth, leap);
printHeader(currentMonth);
printMonth(numDays, firstDayInCurrentMonth);
cout << endl << endl << endl;
currentMonth = currentMonth + 1;
}
cout << endl;
system("PAUSE");
}
bool isLeapYear(int year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
int firstDayofnewyearMonth(int year)
{
int day_start;
int x1, x2, x3;
x1 = (year - 1) / 4;
x2 = (year - 1) / 100;
x3 = (year - 1) / 400;
day_start = (year + x1 - x2 + x3) % 7;
return day_start;
}
int numOfDaysInMonth(int m, bool leap)
{
if (m == 1) return(31);
else if (m == 2) if (leap) return(29); else return(28);
else if (m == 3) return(31);
else if (m == 4) return(30);
else if (m == 5) return(31);
else if (m == 6) return(30);
else if (m == 7) return(31);
else if (m == 8) return(31);
else if (m == 9) return(30);
else if (m == 10) return(31);
else if (m == 11) return(30);
else if (m == 12) return(31);
else disaster();
}
void printHeader(int m)
{
if (m == 1)
{
skip(7);
cout << "January" << endl;
}
else if (m == 2) { skip(7); cout << "February" << endl; }
else if (m == 3) { skip(7); cout << "March" << endl; }
else if (m == 4) { skip(7); cout << "April" << endl; }
else if (m == 5) { skip(7); cout << "May" << endl; }
else if (m == 6) { skip(7); cout << "June" << endl; }
else if (m == 7) { skip(7); cout << "July" << endl; }
else if (m == 8) { skip(7); cout << "August" << endl; }
else if (m == 9) { skip(7); cout << "September" << endl; }
else if (m == 10) { skip(7); cout << "October" << endl; }
else if (m == 11) { skip(7); cout << "November" << endl; }
else if (m == 12) { skip(7); cout << "December" << endl; }
else disaster();

cout << " S M T W T F S" << endl;
cout << "____________________" << endl;
}
void skip(int i)
{
while (i > 0)
{
cout << " ";
i = i - 1;
}
}
void printMonth(int numDays, int &weekDay)
{
int day = 1;
skipToDay(weekDay);
while (day <= numDays)
{
cout << setw(2) << day << " ";
if (weekDay == 6)
{
cout << endl;
weekDay = 0;
}
else weekDay = weekDay + 1;
day = day + 1;
}
}
void skipToDay(int d)
{
return skip(3 * d);
}
void disaster()
{
cout << "Disaster! Exiting ..." << endl;
exit(-1);
}

[\code]
... user were to input a a day, month, and year it would return what day of the week it is.

using the inbuilt libraries and functions:
http://en.cppreference.com/w/cpp/chrono/c/tm
http://en.cppreference.com/w/cpp/chrono/c/mktime
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
#include <iostream>
#include <chrono>
#include <ctime>

void getDay (int year, int mon, int day)//suggest input validation
{
    struct std::tm t;
    t.tm_sec = 0;
    t.tm_min = 1;
    t.tm_hour = 1;
    t.tm_mday = day;       // day of month (1 .. 31)
    t.tm_mon = mon-1;      // month of year (0 .. 11)
    t.tm_year = year-1900; // year since 1900
    t.tm_isdst = -1;       // determine whether daylight saving time
    std::time_t tt = std::mktime(&t);
    if (tt == -1) {
        throw "no valid system time";
    }
    switch(t.tm_wday)
    {
        case 0: std::cout << "Sun \n"; break;
        case 1: std::cout << "Mon \n"; break;
        case 2: std::cout << "Tue \n"; break;
        case 3: std::cout << "Wed \n"; break;
        case 4: std::cout << "Thu \n"; break;
        case 5: std::cout << "Fri \n"; break;
        case 6: std::cout << "Sat \n"; break;
        default: std::cout << " \n"; break;
    }
}

int main()
{
   getDay(2017, 06, 10);
}

This is not quite what I'm trying to do. I want the user to enter the year, the month, and the day. Then it uses the numbers to figure out what day of the week it is.

P.S. - Your program kept telling me it was Saturday.
Gunnerfunner,

this is what I have so far. I was wondering why it wont accept any date after 1970. Also is it possible to limit the months to the number of days in that month?

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
#include<iostream>
#include<cmath>
#include<string>
#include<ctime>
#include<chrono>

using namespace std;

int main()
{
	int month;
	int day;
	int year;
	int time;
	char confirm;

	cout << "Year(Any Year After and Including 1970):  ";
	cin >> year;
	cout << "Month(1 - 12):  ";
	cin >> month;
	cout << "Day:  ";
	cin >> day;
	cout << "Time(24 Hour Format):  ";
	cin >> time;

	struct std::tm t;
	t.tm_sec = 0;
	t.tm_min = 1;
	t.tm_hour = 1;
	t.tm_mday = day;       // day of month (1 .. 31)
	t.tm_mon = month - 1;      // month of year (0 .. 11)
	t.tm_year = year - 1900; // year since 1900
	t.tm_isdst = -1;       // determine whether daylight saving time
	time_t tt = mktime(&t);
	if (tt == -1)
	{
		throw "No valid system time";
	}

	switch (t.tm_wday)
	{
	case 0:
	{
		cout << "Sun \n";
		if (time >= 0 && time < 500)
		{
			cout << "Sorry we aren't open yet.\n";
		}
		else if (time >= 2300 && time <= 2400)
		{
			cout << "Sorry we are closed for the day.\n";
		}
		else if (time >= 500 && time <= 2300)
		{
			cout << "Congrats!  We are open for now.\n";
			break;
		}
	case 1:
	{
		cout << "Mon \n";
		if (time >= 0 && time < 500)
		{
			cout << "Sorry we aren't open yet.\n";
		}
		else if (time >= 2300 && time <= 2400)
		{
			cout << "Sorry we are closed for the day.\n";
		}
		else if (time >= 500 && time <= 2300)
		{
			cout << "Congrats!  We are open for now.\n";
			break;
		}
	case 2:
	{
		cout << "Tue \n";
		if (time >= 0 && time < 500)
		{
			cout << "Sorry we aren't open yet.\n";
		}
		else if (time >= 2300 && time <= 2400)
		{
			cout << "Sorry we are closed for the day.\n";
		}
		else if (time >= 500 && time <= 2300)
		{
			cout << "Congrats!  We are open for now.\n";
			break;
		}
	case 3:
	{
		cout << "Wed \n";
		if (time >= 0 && time < 500)
		{
			cout << "Sorry we aren't open yet.\n";
		}
		else if (time >= 2300 && time <= 2400)
		{
			cout << "Sorry we are closed for the day.\n";
		}
		else if (time >= 500 && time <= 2300)
		{
			cout << "Congrats!  We are open for now.\n";
			break;
		}
	case 4:
	{
		cout << "Thu \n";
		if (time >= 0 && time < 500)
		{
			cout << "Sorry we aren't open yet.\n";
		}
		else if (time >= 2300 && time <= 2400)
		{
			cout << "Sorry we are closed for the day.\n";
		}
		else if (time >= 500 && time <= 2300)
		{
			cout << "Congrats!  We are open for now.\n";
			break;
		}
	case 5:
	{
		cout << "Fri \n";
		if (time >= 0 && time < 500)
		{
			cout << "Sorry we aren't open yet.\n";
		}
		else if (time >= 2300 && time <= 2400)
		{
			cout << "Sorry we are closed for the day.\n";
		}
		else if (time >= 500 && time <= 2300)
		{
			cout << "Congrats!  We are open for now.\n";
			break;
		}
	}
	case 6:
	{
		cout << "Sat \n";
		if (time >= 0 && time < 500)
		{
			cout << "Sorry we aren't open yet.\n";
		}
		else if (time >= 2300 && time <= 2400)
		{
			cout << "Sorry we are closed for the day.\n";
		}
		else if (time >= 500 && time <= 2300)
		{
			cout << "Congrats!  We are open for now.\n";
			break;
		}
	default:
		cout << "Coming Soon";
		break;
	}

	cout << "Welcome to the Roadkill Cafe." << endl;
	}
	}
	}
	}
	}
	}
	system("PAUSE");
	return 0;
}


RCUniversal
Topic archived. No new replies allowed.