+ 1
How can I add a time delay to c++ codes?
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.
+ 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
0
sorry guys but your answers weren't helpful