forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch
61 lines (54 loc) · 1.67 KB
/
stopwatch
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
#include <iostream>
#include <ctime>
//<ctime> headerfile/set of functions, macros and types to work with date and time.
#include <chrono>
//chrono is a headerfile ./and type of function to work with time
using namespace std;
using namespace std::chrono;
// std::chrono::duration represents a time interval
int main()
{
bool is_running = false;
time_t start_time, end_time;
double elapsed_time = 0;
//elapsed time:two timestamps/ at the beginning of the code and the other at the end.
while (true) {
cout << "Press s to start, p to stop, r to reset, or q to quit: ";
char input;
cin >> input;
if (input == 's') {
if (!is_running) {
start_time = time(nullptr);
is_running = true;
cout << "Stopwatch started." << endl;
}
else {
cout << "Stopwatch is already running." << endl;
}
}
else if (input == 'p') {
if (is_running) {
end_time = time(nullptr);
elapsed_time += difftime(end_time, start_time);
is_running = false;
cout << "Elapsed time: " << elapsed_time << " seconds." << endl;
}
else {
cout << "Stopwatch is not running." << endl;
}
}
else if (input == 'r') {
is_running = false;
elapsed_time = 0;
cout << "Stopwatch reset." << endl;
}
else if (input == 'q') {
cout << "Goodbye!" << endl;
break;
}
else {
cout << "Invalid input." << endl;
}
}
return 0;
}