-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cpp
195 lines (164 loc) · 5.22 KB
/
clock.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
/*
* clock.cpp
*
* Date: 09/15/2023
* Author: Robert Rak
*/
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cmath>
using namespace std;
// Input variables
string input;
vector<std::string> tokens;
string token;
// Misc declarations
char delimiter = ':';
vector<int> time_clock(3);
bool quit = false;
int menu_choice;
int hour;
enum Period
{
HOUR = 1,
MINUTE = 2,
SECOND = 3
};
// Initializes the time vector, where position 0 contains the hours, position 1, the minutes, and position 2, the seconds
void initialize_time(vector<int>& time) {
time.clear();
// User enters time in the format of "hours:minutes:seconds" to initialize the clock (ex. 3:22:1 or 03:22:01)
cout << "Please enter the current time in 24-hour format (ex. 15:22:01 or 15:22:1) to initialize the clock: " << endl;
getline(std::cin, input);
istringstream iss(input);
// Handle splitting based on delimeter
while (getline(iss, token, delimiter)) {
tokens.push_back(token);
}
// Convert string tokens to integers
for (int i = 0; i < 3; ++i) {
switch (i) {
case 0:
time[0] = stoi(tokens.at(i));
if (time[0] > 23) time[0] = 23; // Bounds checking
break;
case 1:
time[1] = stoi(tokens.at(i));
if (time[1] > 59) time[1] = 59; // Bounds checking
break;
case 2:
time[2] = stoi(tokens.at(i));
if (time[2] > 59) time[2] = 59; // Bounds checking
break;
}
}
}
// Increases time according to type provided, while also handling overflow
void increment_time(vector<int>& time, Period period) {
switch (period) {
case HOUR:
if (time[0] == 24) {
time[0] = 1;
}
else time[0] += 1;
break;
case MINUTE:
if (time[1] == 59) {
time[1] = 0;
time[0] += 1;
}
else time[1] += 1;
break;
case SECOND:
if (time[2] == 59) {
time[2] = 0;
time[1] += 1;
}
else time[2] += 1;
// Handle cascade (if minutes happen to be 59 at the same time as seconds)
if (time[1] == 60) {
time[1] = 0;
time[0] += 1;
}
if (time[0] == 25) {
time[0] = 1;
}
break;
}
}
// Prints the 12 and 24 hour clocks alongside each other
void print_time(vector<int>& time) {
const char* clock_header = R"(
************************** **************************
* 12-Hour Clock * * 24-Hour Clock *
)";
const char* clock_footer = R"(
************************** **************************
)";
// Handle special case conversion of 12-hour clock
hour = time[0];
if (hour > 12) hour = abs(hour - 12);
// Expressions here determine when to add "0" to a string after conversion, which is when the integer is less than ten.
string twenty_four_hours = ((time[0] < 10) ? ("0" + to_string(time[0])) : to_string(time[0]));
string twelve_hours = ((hour < 10) ? ("0" + to_string(hour)) : to_string(hour));
string minutes = ((time[1] < 10) ? ("0" + to_string(time[1])) : to_string(time[1]));
string seconds = ((time[2] < 10) ? ("0" + to_string(time[2])) : to_string(time[2]));
// Format the two different times
string twelve_time = twelve_hours + ":" + minutes + ":" + seconds + " " + (time[0] < 12 ? "AM" : "PM");
string twenty_four_time = twenty_four_hours + ":" + minutes + ":" + seconds;
// Combine into final output
cout << clock_header;
cout << "* " << twelve_time << " *";
cout << " ";
cout << "* " << twenty_four_time << " *";
cout << clock_footer << endl;
}
// Displays the user menu
void show_menu() {
const char* menu = R"(
**************************
* 1 -- Add One Hour *
* 2 -- Add One Minute *
* 3 -- Add One Second *
* 4 -- Exit Program *
**************************
)";
cout << menu << endl;
}
int main()
{
initialize_time(time_clock);
while (quit != true) {
print_time(time_clock);
show_menu();
cout << "Menu Choice: ";
cin >> menu_choice;
// Handles non-numeric input
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid menu choice! Valid choices are between 1 through 4." << endl;
cin >> menu_choice;
}
switch (menu_choice) {
case 1:
increment_time(time_clock, Period::HOUR);
break;
case 2:
increment_time(time_clock, Period::MINUTE);
break;
case 3:
increment_time(time_clock, Period::SECOND);
break;
case 4:
quit = true;
break;
default:
cout << "Invalid menu choice! Valid choices are between 1 through 4." << endl;
break;
}
}
return 0;
}