Digital clock program in c++

 Welcome back, readers! this post is going to be very special. We are going to discuss a c++ program to build a digital clock in c++. It will help us to practice conditional statements and loops which are essential concepts of c++ programming. Our digital clock program will get local time using time() function in "time_t" and "tm" type variables. The "tm" structure has date and time attributes that we will use to initialize our variables. Finally, we will avail an infinite while loop to build an amazing digital clock program in c++

How to build a digital clock program in c++?

Let's first break down the digital clock program into smaller steps to make it easy and comprehensive. We need to follow the following steps.
  • Get the current system time using the time() function.
  • Declare hours, min, and sec variables and initialize with tm structure attributes.
  • Display the digital clock time inside an infinite while loop.
  • Increment the values of hours, min, and sec variables based on specific conditions.
  • Add delay and clear previous time from screen.
Let's discuss all these steps in detail.

How to get the current system time?

  • Use the time() function available in ctime library in c++. It returns the current time as an object of time_t type. 
  • Convert the time_t type to tm type by localtime() function which takes the reference of time_t identifier as an argument.
  • Declare a tm type pointer "timePtr" to store the result of the localtime() function.
  • The tm structure type has different attributes like tm_sec, tm_min, tm_hour, etc that we can use for time manipulation.
    //getting local time
    time_t t = time(NULL);
    tm *timePtr = localtime(&t);
   	


How to use tm struct attributes?

  • Since "timeptr" is a pointer to an object of the struct tm, we can access the attributes through the arrow operator.
  • Declare sec variable and initialize it with tm_sec attribute.
  • Declare another variable min and initialize it with tm_mim attribute.
  • Initialize hours variable with tm_hour attribute.
  • Declare timestr variable to store AM and PM.
  • If the hours variable is greater than 12, set it to AM. Otherwise set to PM. 
       //store local time in variables
        int sec = (timePtr->tm_sec);
        int min = (timePtr->tm_min);
        int hours = (timePtr->tm_hour);
        string timestr;
       	//convert time to 12 hrs format
        if (hours > 12)
        {
            hours = hours % 12;
            timestr = "PM";
        }
        else
        {
            timestr = "AM";
        }
    


How to display digital clock to the user?

  • Use a while loop with true instead of any condition to create an infinite loop.
  • Print the values of variable hours, min, and sec.
  • Print proper spaces and lines to make it look nice.
        while (true)
        {
            system("cls");
           	//printing time
            cout << "\n\nProgrammopedia Clock Local time\n";
            cout << "_______________________________\n\n";
            cout << "      |" << hours << " : " << min << " : " << sec << " " << timestr << " |" << endl;
            cout << "_______________________________\n\n";
        }


How to increment digital clock time?

  • Increment the sec variable on every iteration of the while loop.
  • Once the value of sec reaches 59, increment the min variable by 1 and set the sec back to zero.
  • In the same way, when the min reaches 59, increment hours by 1 and set the min variable to 0.
  •  Set the hours to 1, when it reaches 12.
        while (true)
        {
           	//increment sec min and hours	
            sec++;
            if (sec > 59)
            {
                sec = 0;
                min++;
            }
    
            if (min > 59)
            {
                min = 0;
                hours++;
            }
    
            if (hours > 12)
            {
                hours = 1;
            }
        }
    
    


How to add delay and clear screen?

  • Use the system("cls") function to clear the previous staff from the screen.
  • Use Sleep() to add delay to our digital clock program.
  • Pass 1000 to add a delay of one second.
  • The sleep() function is available in the window.h header file.

    while (true)
    {
        system("cls");
       	//printing time
        cout << "\n\nProgrammopedia Clock Local time\n";
        cout << "_______________________________\n\n";
        cout << "      |" << hours << " : " << min << " : " << sec << " " << timestr << " |" << endl;
        cout << "_______________________________\n\n";

       	//increment sec min and hours	
        sec++;
        if (sec > 59)
        {
            sec = 0;
            min++;
        }

        if (min > 59)
        {
            min = 0;
            hours++;
        }

        if (hours > 12)
        {
            hours = 1;
        }

        Sleep(1000);
    }



C++ program to build digital clock in c++?

We already learned the digital clock program in c++. Now let's see the complete code of all the steps at once.

#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;

int main()
{
   	//getting local time
    time_t t = time(NULL);
    tm *timePtr = localtime(&t);
   	//store local time in variables
    int sec = (timePtr->tm_sec);
    int min = (timePtr->tm_min);
    int hours = (timePtr->tm_hour);
    string timestr;
   	//convert time to 12 hrs format
    if (hours > 12)
    {
        hours = hours % 12;
        timestr = "PM";
    }
    else
    {
        timestr = "AM";
    }

    while (true)
    {
        system("cls");
       	//printing time
        cout << "\n\nProgrammopedia Clock Local time\n";
        cout << "_______________________________\n\n";
        cout << "      |" << hours << " : " << min << " : " << sec << " " << timestr << " |" << endl;
        cout << "_______________________________\n\n";

       	//increment sec min and hours	
        sec++;
        if (sec > 59)
        {
            sec = 0;
            min++;
        }

        if (min > 59)
        {
            min = 0;
            hours++;
        }

        if (hours > 12)
        {
            hours = 1;
        }

        Sleep(1000);
    }

    return 0;
}

Output of the program:
digital clock program in c++

Digital clock program in c++ using asctime?

In the first program, we took the current system time and save it in our own variables. Then we incremented the variables in a specific pattern in a while loop to run our digital clock. But this time we are using another approach.
  • We are going to get the current system time inside a loop.
  • Print the time to the user using asctime() function. This function converts the content of tm struct to a human-readable string.
  • Add a delay of one second using the sleep() function.
  • Use system("cls") to clear previous content.
In this way, the asctime() function will print the latest time fetched inside a while loop after one sec delay.

#include<iostream>
#include <ctime>
#include<windows.h>

using namespace std;

int main() {

    while (true) {
        system("cls");
        //getting local time
        time_t t = time(NULL);
        tm * timePtr = localtime( & t);
        //printing time
        cout << "\n\nProgrammopedia Clock Local time\n";
        cout << "_______________________________\n\n";
        cout << "    " << asctime(timePtr) << endl;
        cout << "_______________________________\n\n";
        Sleep(1000);
    }

    return 0;
}

Output of the program:
digital clock program in c++


Digital clock program in c++ using ctime?

  • Now, in this case, we are using the ctime() function to print the time to the user.
  • We can prevent the call to localtime() and asctime() by just calling ctime() function. 
  • It is the combination of both localtime() and asctime() functions.

#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;

int main()
{
    while (true)
    {
        system("cls");
       	//getting local time
        time_t t = time(NULL);
       	//printing time
        cout << "\n\nProgrammopedia Clock Local time\n";
        cout << "_______________________________\n\n";
        cout << "    " << ctime(&t) << endl;
        cout << "_______________________________\n\n";
        Sleep(1000);
    }

    return 0;
}


Output of the program:


digital clock program in c++

Recommended:

Now it's time to end our post. We learned how to build a digital clock program in c++ using three different methods. I hope you liked it. Thanks for reading our content and supporting us.

Post a Comment

0 Comments