Hey guys, I would like to learn to put a delay between output and input statements.
How can I do this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main()
{
cout << "This is one line of text" << endl;
// Wait about 3-5 seconds
cout << "I want this line of text to display about 5 seconds after the first line" << endl;
return 0;
}
Duoas, I thought someone said that usleep() is deprecated and nanosleep() is better, but I could be wrong..
Would you say that this implementation is better than http://en.cppreference.com/w/cpp/thread/sleep_for ? I was going to use sleep for an updating program and wasn't sure which to choose.
Yes, POSIX has dropped usleep() for nanosleep(), but usleep() isn't going away any time soon. (Untold numbers of programs rely on it.) And particularly as nanosleep() is quite a bit more clunky to use.
In a POSIX environment, you could also select() on a NULL file descriptor set for a certain amount of time.
The this_thread.sleep_for( N ) is preferred in newer C++11 compilers.