+ 1

How to add a time delay in c++?

I want to add a time delay b/w two commands in c++. I'll prefer a function with its header file,than a loop. I've tried many but none worked.

16th Jul 2017, 12:47 PM
Kanishk Singh Bisht
Kanishk Singh Bisht - avatar
7 Answers
+ 9
first way: use sleep function second way: include <ctime> paste this function: int delay(int milliseconds){ clock_t goal = milliseconds + clock(); while(goal>clock()); return 1; } then call delay function ex: delay(2*1000); cout << "Cow please gimme a milk :(";
16th Jul 2017, 12:54 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
@Martin I found one that works. Because it does not use sleep It just does not show the countdown. https://code.sololearn.com/ckWiLzHgnyJ2/?ref=app
16th Jul 2017, 2:16 PM
Manual
Manual - avatar
+ 2
@Martin You are right while the code is a functional timer the SL compiler does not do it justice.
16th Jul 2017, 5:55 PM
Manual
Manual - avatar
+ 1
I have used sleep function, including <windows.h>. But it didn't worked. The output came at once. Someone suggested me delay(),usleep()....blah blah
16th Jul 2017, 5:41 PM
Kanishk Singh Bisht
Kanishk Singh Bisht - avatar