C++ program to create a stopwatch

In this C++ tutorial, we are going to learn how to create a stopwatch in C++. The stopwatch starts when we press any key on the keyboard and does not stop until any key is pressed. Let’s understand how we can create a stopwatch in C++.

How to create a stopwatch in C++

In this program, we will use a function called _kbhit().

_kbhit(): 

It is a function used to determine if a key has been pressed or not. You have to include conio.h to work with this function. It generally checks if key is pressed or not. If a key is pressed it returns non zero value otherwise zero.

Example program using kbhit() function:

#include<iostream.h>
#include<conio.h>
int main()
{
  while(!kbhit())
   cout<<" you haven't pressed a key.\n";
  return 0;
}

 

In this program, we also use a delay() function.

delay():

This function is used to hold the program execution  for a given number of milliseconds,it is declared in “dos.h” header file.

Syntax:    void delay( unsigned  int milliseconds );

void is a return type function delay()  doesn’t return any value and unsigned int milliseconds is the number of milliseconds required to hold the program.

Here we use delay to see the increment in seconds in the stopwatch clearly. If the delay is of 5000 sec it waits for  5 seconds.

Create a stopwatch in C++

This program has been tested in Turbo C++.

#include<iostream.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

If the above code does not work try this one:

Create a stopwatch in C++ (Works on Windows)

In this program, we also use a Sleep() function.

Sleep():

This function is used to hold the program execution for a given number of milliseconds. it is declared in “Windows.h” header file.

We increment the SS (seconds) variable after the program waits for 1 second and when the seconds variable hits 60, we change it zero and increment the MM (minute) variable. When the minute hits 60 we change it to zero and increment HH (hour) variable.

Example program:

#include<iostream>
#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;
}

And the output of the program will be somewhat like this:

stopwatch
HH: MM: SS
0 : 0 : 0
press any key to start

when you press any key, the stopwatch starts and after 29 seconds, output will be:

stopwatch
HH: MM: SS
0 : 0 : 29
press any key to stop

And in the next second:

stopwatch
HH: MM: SS
0 : 0 : 30
press any key to stop

Now, if you press any key then the stopwatch is paused and the output shows:

stopwatch
HH: MM: SS
0 : 0 : 30
press any key to stop
the time after pausing is
0 : 0 : 30

Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *