how to make a timer in c++ code example
Example 1: add a timer c++
# include <windows.h>
# include <iostream>
using namespace std;
void timer(int sec)
{
Sleep(sec*1000);
}
int main()
{
cout << "Wait 5 seconds " << endl;
timer(5);
cout << "FireWorks !" << endl;
return 0;
}
Example 2: timer in c++
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int hours = 0;
int minutes = 0;
int seconds = 0;
void displayClock()
{
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()
{
while (true) {
displayClock();
sleep(1);
seconds++;
if (seconds == 60) {
minutes++;
if (minutes == 60) {
hours++;
minutes = 0;
}
seconds = 0;
}
}
}
int main()
{
timer();
return 0;
}