We will explore the process of creating a timer in C++ within this educational guide. The timing mechanism initiates upon the pressing of any keyboard key and remains active until another key is pressed. Let's delve into the steps for constructing a timer in C++.
We will employ a function called _kbhit within this program.
_kbhit
It is a functionality that verifies whether a key has been pressed or not. To utilize this feature, you need to include conio.h header file. Typically, it checks for the depression of a key. When a key is pressed, it returns a value that is not zero.
Example of a program utilizing the _kbhit function in C++:
#include<iostream.h>
#include<bits/stdc++.h>
#include<conio.h>
int main ( )
{
while( !kbhit() )
cout<<" you haven't pressed a key.\ n ";
return 0;
}
Output:
You haven't pressed a key.
............................................
Process executed in 1.22 seconds
Press any key to continue.
We can also make use of the delay function in our C++ stopwatch program. This particular function is defined in the "dos.h" header file and serves the purpose of pausing the program's execution for a specific duration in milliseconds.
void delay( unsigned int milliseconds ); //syntax
Unsigned integer milliseconds represents the duration required for the program to pause, with void being the method return type for delay, signifying no return value. In this scenario, we utilize the delay function to effectively observe the second increment on the stopwatch, pausing for 5 seconds when the delay is set to 5000 milliseconds.
Program Breakdown
# include <iostream.h>
# include <bits/stdc++.h>
# include <conio.h>
# include <dos.h>
We will integrate the complete essential library into our program for constructing a stopwatch in C++ to prevent any potential errors.
textbackground( BLUE );
clrscr();
int HH = 0, MM = 0, SS = 0;
cout << " \n\n\\t\t stopwatch";
cout << " \n\t\t HH : MM : SS";
cout << " \n\t\t " << HH << "\t" << MM << "\t" << SS;
cout << " \n\t\t press any key to start";
getch ();
In the provided code snippet, the fundamental appearance of our stopwatch has been established with hours represented by HH, minutes by MM, and seconds by SS.
while ( !kbhit() )
{
SS++;
delay ( 1000 );
if ( SS > 59 )
{
MM++;
SS = 0;
}
if ( MM > 59 )
{
HH++;
MM = 0;
}
clrscr ();
cout << " \n\t\t stopwatch";
cout << " \n\t\t HH : MM : SS";
cout << " \n\t\t " << HH << " : " << MM << " : " << SS;
cout << " \n\t\t press any key to stop";
}
In the provided C++ code snippet, the while loop will continue to run as long as the condition for the kbhit function evaluates to false, indicating whether a key press has occurred or not. This functionality serves to detect keyboard input status. To utilize this capability, the inclusion of the conio library is necessary. Typically, it confirms the occurrence of a key press. Upon pressing a key, a non-zero value is returned by the function.
getch ();
cout << "\n\t\t the time after pausing is" : cout << "\n\t\t" << HH << " : " << MM << " : " << SS;
getch ();
}
The function getch is an abbreviation of "get character" and is part of the conio.h library with a predefined function called "it." When working with Turbo C or Turbo C++, we employ getch to display characters to the user.
Program for creating Stopwatch in C++
# include <iostream.h>
# include <bits/stdc++.h>
# include <conio.h>
# include <dos.h>
void main ()
{
textbackground( BLUE );
clrscr();
int HH = 0, MM = 0, SS = 0;
cout << " \n\n\\t\t stopwatch";
cout << " \n\t\t HH : MM : SS";
cout << " \n\t\t " << HH << "\t" << MM << "\t" << SS;
cout << " \n\t\t press any key to start";
getch ();
while ( !kbhit() )
{
SS++;
delay ( 1000 );
if ( SS > 59 )
{
MM++;
SS = 0;
}
if ( MM > 59 )
{
HH++;
MM = 0;
}
clrscr ();
cout << " \n\t\t stopwatch";
cout << " \n\t\t HH : MM : SS";
cout << " \n\t\t " << HH << " : " << MM << " : " << SS;
cout << " \n\t\t press any key to stop";
}
getch ();
cout << "\n\t\t the time after pausing is" : cout << "\n\t\t" << HH << " : " << MM << " : " << SS;
getch ();
}
Output:
stopwatch
HH : MM : SS
0 : 0 : 22
press any key to stop
the time after pausing is
0 : 0 : 22
Try executing the following code if the previous one fails to work in your integrated development environment (IDE):
We also make use of the Sleep method within our program.
Sleep
This function pauses the program's execution for a designated number of milliseconds. It is defined in the "Windows.h" header file.
After a one-second delay, the program proceeds to increase the SS (seconds) variable. If the seconds value hits 60, it resets to zero and the MM (minute) variable is incremented. Similarly, when the minutes reach 60, they reset to zero and the HH (hour) variable is advanced.
Program Breakdown
# include <iostream>
# include <bits/stdc++.h>
# include <conio.h>
# include <Windows.h>
We will need to include all the required header files, particularly the windows.h file, which controls the program's execution for a set duration of milliseconds.
system ( "CLS" );
int HH = 0, MM = 0, SS = 0;
cout << " \n\t\t stopwatch";
cout << " \n\t\t HH: MM: SS";
cout << " \n\t\t " << HH << " : " << MM << " : " << SS;
cout << " \n\t\t press any key to start";
_getch ();
Here, as demonstrated in the previously discussed stopwatch code, we have established the fundamental design of our stopwatch where hours are represented by HH, minutes by MM, and seconds by SS.
while ( !_kbhit() )
{
SS++;
Sleep( 1000 );
if ( SS > 59 )
{
MM++;
SS = 0;
}
if ( MM > 59 )
{
HH++;
MM = 0;
}
system( "CLS" );
cout << " \n\t\t stopwatch";
cout << " \n\t\t HH: MM: SS";
cout << " \n\t\t " << HH << " : " << MM << " : " << SS;
cout << " \n\t\t press any key to stop";
}
In the provided C++ code snippet, the while loop will continue to run as long as the condition for the function kbhit being false remains true. This function serves to determine whether a key press has occurred. In order to utilize this functionality, one must include the conio library. Typically, it evaluates whether a key has been pressed by returning a non-zero value when a key press is detected.
Another Program for Stopwatch in C++
# include <iostream>
# include <bits/stdc++.h>
# include <conio.h>
# include <Windows.h>
using namespace std;
int main ()
{
system ( "CLS" );
int HH = 0, MM = 0, SS = 0;
cout << " \n\t\t stopwatch";
cout << " \n\t\t HH: MM: SS";
cout << " \n\t\t " << HH << " : " << MM << " : " << SS;
cout << " \n\t\t press any key to start";
_getch ();
while ( !_kbhit() )
{
SS++;
Sleep( 1000 );
if ( SS > 59 )
{
MM++;
SS = 0;
}
if ( MM > 59 )
{
HH++;
MM = 0;
}
system( "CLS" );
cout << " \n\t\t stopwatch";
cout << " \n\t\t HH: MM: SS";
cout << " \n\t\t " << HH << " : " << MM << " : " << SS;
cout << " \n\t\t press any key to stop";
}
_getch ();
cout << " \n\t\t the time after pausing is";
cout << " \n\t\t " << HH << " : " << MM << " : " << SS;
_getch ();
return 0 ;
}
Output:
stopwatch
HH: MM: SS
0 : 0 : 0
press any key to start
When a keyboard key is pressed, a timer initiates, and upon reaching 29 seconds, the subsequent result is shown:
stopwatch
HH: MM: SS
0 : 0 : 29
press any key to stop
And immediately after:
stopwatch
HH: MM: SS
0 : 0 : 30
press any key to stop
Pressing any key will stop the timer and showcase the subsequent result:
stopwatch
HH: MM: SS
0 : 0 : 30
press any key to stop
the time after pausing is
0 : 0 : 30