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;
}
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;
}
c++ timer
#include <iostream>
#include <chrono>
// This is how to measure the time it takes functions to finish
long add(int a, int b) {
return a + b;
}
int main() {
auto start = std::chrono::steady_clock::now();
std::cout << "9 + 10 = " << add(9, 10) << '\n';
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "elapsed time to compute 9 + 10: " << elapsed_seconds.count() << "s\n";
return 0;
}
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;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us