+ 1

How can I add a time delay to c++ codes?

10th Feb 2017, 10:29 AM
Zack codez
Zack codez - avatar
4 Answers
+ 8
If you're on Windows OS, you may include the header file <windows.h> and use the Sleep() function. E.g. cout << "Hello "; Sleep(1000); cout << "World."; The sleep timer is calculated in milliseconds. Yet again, there is probably a more standard way which I know not of.
10th Feb 2017, 10:44 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
An OS independent version of the sleep or delay function. bool Wait(const unsigned long &Time) { clock_t Tick=clock_t(float(clock())/float(CLOCKS_PER_SEC)*1000.f); if(Tick<0) { return 0; } clock_t Now=clock_t(float(clock())/float(CLOCKS_PER_SEC)*1000.f); if(Now<0) { return 0; } while((Now - Tick)<Time) { Now=clock_t(float(clock())/float(CLOCKS_PER_SEC)*1000.f); if(Now<0) return 0; } return 1; } But if you are doing it on an online compiler like in code playground of sololearn then no command or function can help you as they show whole output in once
10th Feb 2017, 12:01 PM
Megatron
Megatron - avatar
0
sorry guys but your answers weren't helpful
10th Feb 2017, 11:37 AM
Zack codez
Zack codez - avatar