A C++ timer serves as a tool for measuring time spans, monitoring the length of processes, or introducing pauses within a software application. Timers have diverse applications, including integrating time-dependent features, overseeing animations, calculating the runtime of algorithms, and organizing activities at set time intervals.
Example:
Let's consider a sample program to demonstrate the usage of Timers in C++.
#include<iomanip>
#include<iostream>
#include <cstdlib>
#include<unistd.h>
using namespace std;
int hours =0;
int minutes =0;
int seconds =0;
void timerDisplay(){
system("cls");
cout << "Hours :" << hours << "Minutes :" << minutes << "Seconds" << seconds << endl;
}
void timerFunction(){
while(true){
timerDisplay();
sleep(1);
seconds = seconds + 1;
if(seconds == 60){
minutes = minutes + 1;
if(minutes == 60){
hours = hours + 1;
minutes = 0;
}
seconds = 0;
}
}
}
int main(){
timerFunction();
return 0;
}
Output:
Explanation:
Include statements:
- Header files like "iomanip" is used for manipulators that format the output.
- iostream is used to include the input and output stream operations.
- cstdlib header file contains functions involving memory allocation and random numbers.
- h header file is for POSIX operating system APIs. It contains the sleep function.
timerDisplay function:
- This function is used to show the current time on the console.
- In this function, we use the statement system("cls") , which is used to clear the console. Before printing the current statements, it will remove the console and print the new statement.
- The current hours, minutes, and seconds are displayed by using the cout
timerFunction function:
This function comprises the primary program code responsible for executing the timer feature. It consists of an endless loop, a while loop that continuously updates the timer. It invokes a separate function called timerDisplay to display the updated time. Following this, the program pauses for one second using the sleep(1) function, allowing the seconds to increment. If the seconds tally reaches 60, they reset to 0, and the minutes are then incremented. This same principle is applied to minutes and hours as well.
Main function:
This function starts the timer by invoking the timerFunction method. The application will continuously execute, showing the timer on the console until it is manually halted.
Example:
Let's consider a different program to demonstrate the usage of Timers in C++.
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
using namespace std;
void timerDisplay(int hours, int minutes, int seconds) {
cout << setw(2) << setfill('0') << hours << ":";
cout << setw(2) << setfill('0') << minutes << ":";
cout << setw(2) << setfill('0') << seconds << endl;
}
int main() {
int hours = 0;
int minutes = 0;
int seconds = 0;
while (true) {
this_thread::sleep_for(chrono::seconds(1));
seconds = seconds + 1;
if (seconds == 60) {
seconds = 0;
minutes = minutes + 1;
if (minutes == 60) {
minutes = 0;
hours = hours + 1;
}
}
timerDisplay(hours, minutes, seconds);
}
return 0;
}
Output:
Explanation:
This software also incorporates the C++ timer functionality. Moving forward, let's delve into the breakdown of the aforementioned program. The function consists of 3 distinct variables: hours, minutes, and seconds. These variables correspond to the count of hours, minutes, and seconds in the timer, respectively.
There exist two functions within the program: timerDisplay and the main function.
timerDisplay function:
This function receives three parameters and has a void return type. It is designed to showcase the current time in the HH: MM: SS format. To achieve this, it leverages setw(2) and setfill('0') to guarantee that the hours, minutes, and seconds are shown with leading zeros when their values are below 10.
main function:
Its return type is integer. In this case, hours, minutes, and seconds are set to zero initially. The function includes a while loop that increments the time and incorporates a sleep method that halts the program for one second employing the chrone library. When the seconds reach 60, they are reset to 0, and the minutes are increased. If the minutes reach 60, they are reset to 0, and the hours are increased. The Display method invokes the timerDisplay function to exhibit the current time in HH:MM:SS format.
Conclusion:
In summary, the provided C++ timer examples demonstrate two distinct methods for calculating time durations. The initial method involves using system calls and OS functions such as system("cls") and sleep(1) to construct a straightforward timer that shows hours, minutes, and seconds. This technique imparts fundamental knowledge about time tracking and manipulating the console interface.
The alternative method utilizes the C++ chrono library, offering a more accurate and sophisticated solution. By leveraging std::chrono for time management and using thisthread::sleepfor(chrono::seconds(1)) for pauses, this technique showcases contemporary C++ strategies for timer integration, improving precision and clarity.