We will learn how to make a timer in C++ in this tutorial. The stopwatch begins when any key on the keyboard is pressed, and it doesn't end until another key is pressed. Let's learn how to build a timer in C++.
We'll utilize a function named _kbhit in this program.
_kbhit
It is a feature that checks if a key has been depressed or not. To use this function, conio.h must be included. Usually, it verifies if a key has been depressed. When a key is pushed, a value other than zero is returned.
Example of a program using _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 utilize the delay method in our program of stopwatch in C++. This function, which is declared in the "dos.h" header file, is used to delay the execution of the program for a predetermined amount of milliseconds.
void delay( unsigned int milliseconds ); //syntax
Unsigned int milliseconds is the amount of milliseconds needed to hold the programme, while void is the return type for the method delay, which returns nothing. Here, we make advantage of delay to clearly notice the second increment in the stopwatch. It waits 5 seconds if the delay is 5000 seconds.
Program Breakdown
# include <iostream.h>
# include <bits/stdc++.h>
# include <conio.h>
# include <dos.h>
We will include the entire necessary library in our program to build a stopwatch in C++ to avoid any type of error.
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 above peace of code, we have implemented the basic look of our stopwatch having hours denoted by HH and minutes are denoted by MM and seconds as 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 above mentioned code in C++, the while lope while executed if the function kbhit is not true i.e. It is a feature that checks if a key has been depressed or not. To use this function, conio must be included. Usually, it verifies if a key has been depressed. When a key is pushed, a value other than zero is returned.
getch ();
cout << "\n\t\t the time after pausing is" : cout << "\n\t\t" << HH << " : " << MM << " : " << SS;
getch ();
}
getch is short for get character, conio.h library has a predefined function named "it." In Turbo C or Turbo C++, we utilize getch to show the 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 the below code if the one above does not work on you IDE:
We also utilize the Sleep method in our program.
Sleep
This function holds the execution of the program for a specified amount of milliseconds. It is stated in the header file "Windows.h".
After the program waits for 1 second, we increment the SS (seconds) variable. When the second variable reaches 60, we set it to zero and increment the MM (minute) variable. We reset the minute to zero when it reaches 60 and increase the HH (hour) variable.
Program Breakdown
# include <iostream>
# include <bits/stdc++.h>
# include <conio.h>
# include <Windows.h>
We going to include the entire necessary header file specially the windows.h which holds the execution of the program for a specified amount 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 we done in above explained code of stopwatch, we have implemented the basic look of our stopwatch having hours denoted by HH and minutes are denoted by MM and seconds as 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 above mentioned code in C++, the while lope while executed if the function kbhit is not true i.e. It is a feature that checks if a key has been depressed or not. To use this function, conio must be included. Usually, it verifies if a key has been depressed. When a key is pushed, a value other than zero is returned.
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 key is pressed, a timer begins, and after 29 seconds, the following output is displayed:
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
Currently, pressing any key will halt the stopwatch and display the following output:
stopwatch
HH: MM: SS
0 : 0 : 30
press any key to stop
the time after pausing is
0 : 0 : 30