timer in c++
// CPP program to create a timer
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
// hours, minutes, seconds of timer
int hours = 0;
int minutes = 0;
int seconds = 0;
// function to display the timer
void displayClock()
{
// system call to clear the screen
system("clear");
cout << setfill(' ') << setw(55) << " TIMER \n";
cout << setfill(' ') << setw(55) << " --------------------------\n";
cout << setfill(' ') << setw(29);
cout << "| " << setfill('0') << setw(2) << hours << " hrs | ";
cout << setfill('0') << setw(2) << minutes << " min | ";
cout << setfill('0') << setw(2) << seconds << " sec |" << endl;
cout << setfill(' ') << setw(55) << " --------------------------\n";
}
void timer()
{
// infinte loop because timer will keep
// counting. To kill the process press
// Ctrl+D. If it does not work ask
// ubuntu for other ways.
while (true) {
// display the timer
displayClock();
// sleep system call to sleep
// for 1 second
sleep(1);
// increment seconds
seconds++;
// if seconds reaches 60
if (seconds == 60) {
// increment minutes
minutes++;
// if minutes reaches 60
if (minutes == 60) {
// increment hours
hours++;
minutes = 0;
}
seconds = 0;
}
}
}
// Driver Code
int main()
{
// start timer from 00:00:00
timer();
return 0;
}
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks