What is the method to tell the console to wait for x seconds. Is there a built in method or must I make one.
Asked
Active
Viewed 8,689 times
3 Answers
7
It's platform specific. On Linux/UNIX, or other POSIX-compliant operating systems, you can use the sleep
function, which takes a parameter in seconds. On Windows you can use Sleep
, which takes a parameter in milliseconds.

Charles Salvia
- 52,325
- 13
- 128
- 140
-
1I get the error, 'sleep': identifier not found – Mohit Deshpande Nov 28 '09 at 19:44
-
Maybe you should include: #include
– Erkan Haspulat Nov 28 '09 at 19:46 -
3See the links to the man pages for each sleep function. You probably need to include the appropriate headers. On Linux, that would be `unistd.h`, on Windows you want `Windows.h` I think. Also, please specify the OS you are using in the future, as it makes it easier for people to answer your questions succinctly. – Charles Salvia Nov 28 '09 at 19:47
2
You are looking for the sleep method.
sleep(5);

Erkan Haspulat
- 12,032
- 6
- 39
- 45
-
1As noted above, the sleep function is not a standard part of C++. – ChrisInEdmonton Nov 28 '09 at 19:49
1
If you want it to be portable, you have to use preprocessing to determine what operating system it is and include the header as appropriate.
It would be good to make a function for calling sleep, like:
void portableSleep(int sec) {
# ifdef POSIX
sleep(sec);
# endif
# ifdef WINDOWS
Sleep(sec * 1000);
# endif
}
Autoconf can help you with this.

alternative
- 12,703
- 5
- 41
- 41