What is the method to tell the console to wait for x seconds. Is there a built in method or must I make one.
+6
A:
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
2009-11-28 19:42:15
I get the error, 'sleep': identifier not found
Mohit Deshpande
2009-11-28 19:44:24
Maybe you should include: #include <windows.h>
Erkan Haspulat
2009-11-28 19:46:07
See 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
2009-11-28 19:47:24
As noted above, the sleep function is not a standard part of C++.
ChrisInEdmonton
2009-11-28 19:49:19
+2
A:
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.
mathepic
2009-11-28 20:03:03