0
How to use delay function?
I just wanna know that is there a function to delay a time in printing a output? please show me whit example!!!
2 Antworten
+ 5
If you use Windows, you may include the <windows.h> header to use Sleep() function.
i.e.
include <iostream>
include <windows.h>
using namespace std;
int main()
{
cout << "Hello" << endl;
Sleep(1000);
cout << "world!" << endl;
return 0;
}
The above code will delay the second cout for 1 second. Value of 1000 is written in the parenthesis because it is calculated in milliseconds.
If you are on an alternate operating system, I am afraid you may have to await input from the others.
Hope I helped. :>
0
It's a bit convoluted, but with C++11 there is a platform independent way to wait for a certain amount of time. Say I wanted to sleep for 4 seconds, I could do:
#include <chrono>
// Create a std::chrono::<duration> object with the time you want to sleep for
std::chrono::seconds timespan(4);
// Pass that into the std::this_thread::sleep_for() function
std::this_thread::sleep_for(timespan);
Before this, the way you did it was platform dependent. Which means that depending upon your OS and environment, your code might not work/compile.