Tutorial :Tell C++ console to wait



Question:

What is the method to tell the console to wait for x seconds. Is there a built in method or must I make one.


Solution:1

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.


Solution:2

You are looking for the sleep method.

sleep(5);  


Solution:3

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »