-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek-5.cpp
More file actions
152 lines (129 loc) · 3.59 KB
/
Copy pathweek-5.cpp
File metadata and controls
152 lines (129 loc) · 3.59 KB
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
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <functional>
#include <exception>
#include <string>
using namespace std;
const string possibleMonths[12] = {
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december",
};
class invalid_input: public exception {
private:
string message;
public:
explicit invalid_input(string msg) : message(std::move(msg)) { }
public:
string what() {
return message;
}
};
bool isMonthValid(const string& monthInput) {
for(const string& possibleMonth : possibleMonths) {
if (possibleMonth == monthInput) {
return true;
}
}
return false;
}
int findMaxDays(int year, const string& month) {
int yearDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
yearDays[1] = 29;
}
for (int i = 0; i < 12; i++) {
if (possibleMonths[i] == month) {
return yearDays[i];
}
}
cout << "Something went wrong, weren't able to determine a max amount of days" << "\n";
return 0;
}
int askForYear() {
string yearInput;
int year;
cout << "Year:";
cin >> yearInput;
year = stoi(yearInput);
return year;
}
string askForMonth() {
string monthInput;
cout << "Month:";
cin >> monthInput;
transform(monthInput.begin(), monthInput.end(), monthInput.begin(),
[](unsigned char c) { return tolower(c); });
if (!isMonthValid(monthInput)) {
throw invalid_input("That's not a month! Try again.");
}
return monthInput;
}
int askForDay(int maxDays) {
string dayInput;
int day;
cout << "Day:";
cin >> dayInput;
day = stoi(dayInput);
if (day <= 0 || day > maxDays) {
throw invalid_input("That day doesn't exist in the given month and year, try again.");
}
return day;
}
int askForHour() {
string hourInput;
int hour;
cout << "Hour:";
cin >> hourInput;
hour = stoi(hourInput);
if (hour < 0 || hour >= 24) {
throw invalid_input("Please enter a valid 24h clock hour. Try again.");
}
return hour;
}
int askForMinute(){
string minuteInput;
int minute;
cout << "Minute:";
cin >> minuteInput;
minute = stoi(minuteInput);
if (minute < 0 || minute > 60) {
throw invalid_input("Please enter a valid minute, between 0 and 60. Try again.");
}
return minute;
}
int main() {
int year, day, hour, minute;
string month;
vector<function<void()>> callList;
callList.emplace_back([&]() { year = askForYear(); });
callList.emplace_back([&]() { month = askForMonth(); });
callList.emplace_back([&]() { day = askForDay(findMaxDays(year, month)); });
callList.emplace_back([&]() { hour = askForHour(); });
callList.emplace_back([&]() { minute = askForMinute(); });
for (const auto& callAction : callList) {
bool isValid = false;
while (!isValid) {
try {
callAction();
isValid = true;
}
catch (const invalid_argument& e) { cout << "That's not a number, try again." << "\n"; }
catch (const out_of_range& e) { cout << "That number is way too big, try again." << "\n"; }
catch (invalid_input& e) { cout << e.what() << "\n"; }
}
}
cout << day << " / " << month << " / " << year << " at " << hour << ":" << minute;
return 0;
}