-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
55 lines (48 loc) · 1.37 KB
/
timer.cpp
File metadata and controls
55 lines (48 loc) · 1.37 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <conio.h>
#include <unistd.h>
void display_time(int hours, int minutes, int seconds) {
printf("\rTime: %02d:%02d:%02d", hours, minutes, seconds);
fflush(stdout);
}
int main() {
int hours = 0, minutes = 0, seconds = 0;
bool running = false;
char command;
printf("Stopwatch - Press 's' to start, 'p' to pause, 'r' to reset, 'q' to quit.\n");
while (1) {
if (running) {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
}
}
display_time(hours, minutes, seconds);
sleep(1);
}
if (kbhit()) {
command = getchar();
if (command == 's') {
running = true;
} else if (command == 'p') {
running = false;
} else if (command == 'r') {
hours = 0;
minutes = 0;
seconds = 0;
running = false;
display_time(hours, minutes, seconds);
} else if (command == 'q') {
break;
}
}
}
return 0;
}