-
Notifications
You must be signed in to change notification settings - Fork 359
/
Find the Day of the Week You Were Born in cpp
255 lines (222 loc) · 8.21 KB
/
Find the Day of the Week You Were Born in cpp
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
#include <iostream>
using namespace std;
int main(void)
{
int DD = 32, MM = 13, YYYY = -1, NYYYY, NMM, IDAY, day, flag = 0;
string month[] = {"January","February","March","April","May","June","July",
"August","September","October","November","December"};
cin >> DD;
cin >> MM;
cin >> YYYY;
//Error Message: User has given no input
if(DD == 32 && MM == 13 && YYYY == -1)
{
cout << " Please enter your birthday in this " << endl;
cout << " format(including spaces): " << endl;
cout << " DD MM YYYY " << endl;
cout << " 22 01 1997 " << endl;
return -1;
}
if(DD <= 0)
{
//Error Message: User has given invalid input for "Days" & "Months" fields
if(MM <= 0)
{
cout << " We don't have negative or null days and months. " << endl;
cout << " Try again! " << endl;
return -1;
}
//Error Message: User has given invalid input for "Days" field
cout << " We don't have negative or null days. " << endl;
cout << " Try again! " << endl;
return -1;
}
if(MM <= 0)
{
//Error Message: User has given invalid input for "Months" field
cout << " We don't have negative or null months. " << endl;
cout << " Try again! " << endl;
return -1;
}
if(DD > 31 || MM > 12 || YYYY <= 0)
{
if(DD > 31 && MM > 12)
{
//Error Message: User has given invalid input for "Days", "Months" & "Years" fields
if(YYYY <= 0)
{
cout << " We have 12 months, the days of a month are up to 31 " << endl;
cout << " and a year should be a positive number. " << endl;
cout << " Try again! " << endl;
}
//Error Message: User has given invalid input for "Days" & "Months" fields
else
{
cout << " We have 12 months and " << endl;
cout << " the days of a month are up to 31. " << endl;
cout << " Try again! " << endl;
}
}
else if(DD > 31 && MM <= 12)
{
//Error Message: User has given invalid input for "Days" & "Years" fields
if(YYYY <= 0)
{
cout << " The days of a month are up to 31 and " << endl;
cout << " a year should be a positive number. " << endl;
cout << " Try again! " << endl;
}
//Error Message: User has given invalid input for "Days" field
else
{
cout << " The days of a month are up to 31. " << endl;
cout << " Try again! " << endl;
}
}
else if(DD <= 31 && MM > 12)
{
//Error Message: User has given invalid input for "Months" & "Years" fields
if(YYYY <= 0)
{
cout << " We have 12 months and " << endl;
cout << " a year should be a positive number. " << endl;
cout << " Try again! " << endl;
}
//Error Message: User has given invalid input for "Months" field
else
{
cout << " We have 12 months. " << endl;
cout << " Try again! " << endl;
}
}
//Error Message: User has given invalid input for "Years" field
else if(DD <= 31 && MM <= 12 && YYYY <= 0)
{
cout << " A year should be a positive number. " << endl;
cout << " Try again! " << endl;
}
return -1;
}
switch(MM)
{
case 2:
if((YYYY % 400) == 0 || ((YYYY % 4) == 0 && (YYYY % 100) != 0))
{
//Error Message: User has requested an invalid day for the month "February"
// and the requested "Year" is a leap year.
if(DD > 29)
{
cout << " The year " << YYYY << " is a leap year. " << endl;
cout << " So, February has up to 29 days. " << endl;
cout << " Try again! " << endl;
return -1;
}
}
else
{
//Error Message: User has requested an invalid day for the month "February"
// and the requested "Year" is not a leap year.
if(DD > 28)
{
cout << " The year " << YYYY << " isn't a leap year. " << endl;
cout << " So, February has up to 28 days. " << endl;
cout << " Try again! " << endl;
return -1;
}
}
break;
case 4:
//Error Message: User has requested an invalid day for the month "April"
if(DD > 30)
{
cout << " April has up to 30 days. " << endl;
cout << " Try again! " << endl;
return -1;
}
break;
case 6:
//Error Message: User has requested an invalid day for the month "June"
if(DD > 30)
{
cout << " June has up to 30 days. " << endl;
cout << " Try again! " << endl;
return -1;
}
break;
case 9:
//Error Message: User has requested an invalid day for the month "September"
if(DD > 30)
{
cout << " September has up to 30 days. " << endl;
cout << " Try again! " << endl;
return -1;
}
break;
case 11:
//Error Message: User has requested an invalid day for the month "November"
if(DD > 30)
{
cout << " November has up to 30 days. " << endl;
cout << " Try again! " << endl;
return -1;
}
break;
}
if(MM <= 2)
{
NYYYY = YYYY - 1;
NMM = 0;
}
else
{
NYYYY = YYYY;
NMM = (4 * MM + 23) / 10;
}
//Calculating the day
IDAY = 365 * YYYY + DD + 31 * (MM - 1) - NMM + (NYYYY / 4) - ((3 * ((NYYYY / 100) + 1) / 4));
day = IDAY % 7;
//This 'flag' is used for displaying the right ending after the numbers
if(DD != 11 && DD != 12 && DD != 13)
flag = DD % 10;
switch(day)
{
case 0:
cout << " You were born on Saturday, ";
break;
case 1:
cout << " You were born on Sunday, ";
break;
case 2:
cout << " You were born on Monday, ";
break;
case 3:
cout << " You were born on Tuesday, ";
break;
case 4:
cout << " You were born on Wednesday, ";
break;
case 5:
cout << " You were born on Thursday, ";
break;
case 6:
cout << " You were born on Friday, ";
break;
}
if(flag == 1){
cout << DD << "st of ";
}
else if(flag == 2){
cout << DD << "nd of ";
}
else if(flag == 3){
cout << DD << "rd of ";
}
else{
cout << DD << "th of ";
}
cout << month[MM-1] << " of " << YYYY << "!" << endl;
cout << "\n And if you liked it, " << endl;
cout << " don't forget to give a (+1) like! " << endl;
cout << " Thank you! " << endl;
return 0;
}