How to Make a Countdown Timer in C++

Suraj P Feb 02, 2024
  1. Make a Count-Down Timer in Unix Environment
  2. Make a Count-Down Timer in Windows
  3. Make an Up Counting Timer in C++
How to Make a Countdown Timer in C++

This article will teach how to make an up and down counting timer in C++. First, let’s see how to make a count-down timer in C++.

The way it works is that we take the minute and seconds from the user as input, and then using loop in our program, we will print the seconds in reverse order or count-down.

We can use a loop like the while loop or the for loop that will count down until the number reaches 1, and inside this, we need a mechanism to pause the program for 1 second to see the change.

We have different functions (system calls) in Unix-like environments and Windows to pause a program or make it sleep. However, they have the same functionality.

Make a Count-Down Timer in Unix Environment

Any Unix-like machine, Linux or Ubuntu, has a function called usleep that takes the time in microseconds to sleep the program. We need to pass the value as 1000000 to pause the execution for one second.

This method is defined in the unistd.h header file.

Example code:

#include <unistd.h>

#include <ctime>
#include <iostream>
using namespace std;

string getPresentDateTime() {
  time_t tt;
  struct tm *st;

  time(&tt);
  st = localtime(&tt);
  return asctime(st);
}

int main() {
  int seconds;
  cout << "Enter total number seconds for the counter" << endl;
  cin >> seconds;

  while (seconds >= 1) {
    cout << "Counter : " << seconds << " : " + getPresentDateTime() << endl;
    usleep(1000000);
    seconds--;
  }
}

In the above program, we use the getPresentDateTime() method to get the current date and time from the system.

Output: The above program was executed in Ubuntu. To execute it in a Unix-like environment, save the file, use g++ to compile it, and type ./a.out to execute it.

g++ first.cpp   //executed in ubuntu
 ./a.out

Enter total number seconds for the counter
7
Counter : 7 : Sun Mar 13 01:31:56 2022

Counter : 6 : Sun Mar 13 01:31:57 2022

Counter : 5 : Sun Mar 13 01:31:58 2022

Counter : 4 : Sun Mar 13 01:31:59 2022

Counter : 3 : Sun Mar 13 01:32:00 2022

Counter : 2 : Sun Mar 13 01:32:01 2022

Counter : 1 : Sun Mar 13 01:32:02 2022

Make a Count-Down Timer in Windows

Everything remains just like we have done above; the only difference is that we must include windows.h instead of unistd.h. And instead of usleep, we have to use the sleep() method.

The sleep() method takes time in milliseconds.

Example code:

#include <Windows.h>

#include <ctime>
#include <iostream>
using namespace std;

string getPresentDateTime() {
  time_t tt;
  struct tm *st;

  time(&tt);
  st = localtime(&tt);
  return asctime(st);
}

int main() {
  int seconds;
  cout << "Enter total number seconds for the counter" << endl;
  cin >> seconds;

  while (seconds >= 1) {
    cout << "Counter : " << seconds << " : " + getPresentDateTime() << endl;
    Sleep(1000);
    seconds--;
  }
}

Output: The above program was run on IDE present on Windows.

Enter total number seconds for the counter
10
Counter : 10 : Sun Mar 13 01:42:13 2022

Counter : 9 : Sun Mar 13 01:42:14 2022

Counter : 8 : Sun Mar 13 01:42:15 2022

Counter : 7 : Sun Mar 13 01:42:16 2022

Counter : 6 : Sun Mar 13 01:42:17 2022

Counter : 5 : Sun Mar 13 01:42:18 2022

Counter : 4 : Sun Mar 13 01:42:19 2022

Counter : 3 : Sun Mar 13 01:42:20 2022

Counter : 2 : Sun Mar 13 01:42:21 2022

Counter : 1 : Sun Mar 13 01:42:22 2022

Now let’s look at an up-counter timer.

Make an Up Counting Timer in C++

It is implemented just like a down counter the only difference is that we run the loop in a forwarding manner, which is an incremental approach.

Example code: Up counter in a Windows environment.

#include <Windows.h>

#include <ctime>
#include <iostream>
using namespace std;

string getPresentDateTime() {
  time_t tt;
  struct tm *st;

  time(&tt);
  st = localtime(&tt);
  return asctime(st);
}

int main() {
  int seconds;
  cout << "Enter total number seconds for the counter" << endl;
  cin >> seconds;

  int count = 0;

  while (count != 10) {
    cout << "Counter : " << seconds << " : " + getPresentDateTime() << endl;
    Sleep(1000);
    seconds++;
    count++;
  }
}

In the above code, we increment the seconds 10 times. And to keep track of it, we have introduced a variable count, which stops when 10 seconds is completed from the time seconds input was given.

Output:

Enter total number seconds for the counter
5
Counter : 5 : Sun Mar 13 02:15:42 2022

Counter : 6 : Sun Mar 13 02:15:43 2022

Counter : 7 : Sun Mar 13 02:15:44 2022

Counter : 8 : Sun Mar 13 02:15:45 2022

Counter : 9 : Sun Mar 13 02:15:46 2022

Counter : 10 : Sun Mar 13 02:15:47 2022

Counter : 11 : Sun Mar 13 02:15:48 2022

Counter : 12 : Sun Mar 13 02:15:49 2022

Counter : 13 : Sun Mar 13 02:15:50 2022

Counter : 14 : Sun Mar 13 02:15:51 2022
Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - C++ Timer