Learn how to create a stopwatch program in C++ to track time effectively.
Introduction
A stopwatch is a common tool used to measure the amount of time elapsed between two events. In this guide, we will walk you through creating a simple stopwatch program in C++. The program will feature basic functionalities such as start, stop, and reset to measure time. This is an excellent beginner-level project for learning how to work with time and loops in C++.
Objective
The goal of this program is to develop a simple stopwatch in C++ that allows a user to:
- Start the stopwatch
- Stop the stopwatch
- Reset the stopwatch
- Display elapsed time in seconds
C++ Code for Simple Stopwatch
#include #include #include using namespace std; using namespace chrono; class Stopwatch { private: steady_clock::time_point start_time; steady_clock::time_point stop_time; bool running; public: Stopwatch() : running(false) {} void start() { if (!running) { start_time = steady_clock::now(); running = true; cout << "Stopwatch started...\n"; } } void stop() { if (running) { stop_time = steady_clock::now(); running = false; cout << "Stopwatch stopped.\n"; } } void reset() { running = false; cout << "Stopwatch reset.\n"; } void display() { if (running) { auto elapsed = duration_cast(steady_clock::now() - start_time); cout << "Elapsed Time: " << elapsed.count() << " seconds\n"; } else { auto elapsed = duration_cast(stop_time - start_time); cout << "Elapsed Time: " << elapsed.count() << " seconds\n"; } } }; int main() { Stopwatch stopwatch; char command; cout << "Simple C++ Stopwatch Program\n"; cout << "Commands: 's' to start, 't' to stop, 'r' to reset, 'd' to display, 'q' to quit\n"; do { cout << "Enter command: "; cin >> command; switch(command) { case 's': stopwatch.start(); break; case 't': stopwatch.stop(); break; case 'r': stopwatch.reset(); break; case 'd': stopwatch.display(); break; case 'q': cout << "Exiting stopwatch program.\n"; break; default: cout << "Invalid command. Try again.\n"; } this_thread::sleep_for(milliseconds(500)); // Brief delay for better interaction experience } while (command != 'q'); return 0; }
Program Explanation
This C++ program defines a Stopwatch
class with the following methods:
- start(): Starts the stopwatch by recording the current time using
steady_clock::now()
. - stop(): Stops the stopwatch and records the current time again.
- reset(): Resets the stopwatch, stopping it if it’s running.
- display(): Displays the elapsed time in seconds, based on whether the stopwatch is running or stopped.
The main()
function takes user input for commands:
's'
: Starts the stopwatch't'
: Stops the stopwatch'r'
: Resets the stopwatch'd'
: Displays the elapsed time'q'
: Quits the program
The program runs in a loop, continually waiting for user input until the 'q'
command is entered to exit. There’s a small delay of 500 milliseconds between inputs to make the interaction smoother.
How to Run the Program
To run this program, follow these steps:
- Open your preferred C++ IDE or a simple text editor.
- Copy and paste the provided C++ code into a new file.
- Save the file with a
.cpp
extension (e.g.,stopwatch.cpp
). - Compile the program using a C++ compiler (e.g.,
g++ stopwatch.cpp -o stopwatch
). - Run the compiled program (e.g.,
./stopwatch
on Unix systems orstopwatch.exe
on Windows). - Use the commands provided in the console to start, stop, reset, and display the stopwatch’s time.