Delaying the program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpm27
    New Member
    • Jan 2008
    • 23

    Delaying the program

    Is there a way in C++ to delay the program (for example before it displays something or calls a function) by a certain amount of seconds? I know it it possible in JavaScript, but is it in C++? Thank you.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Depends on the OS. Windows has a Sleep() where you specify the number of microseconds to sleep.

    Comment

    • Voltem
      New Member
      • Jan 2008
      • 3

      #3
      Originally posted by rpm27
      Is there a way in C++ to delay the program (for example before it displays something or calls a function) by a certain amount of seconds? I know it it possible in JavaScript, but is it in C++? Thank you.
      include the <stdlib.h> library and use the code: Delay( );

      Example:

      [CODE=c]#include <stdlib.h>
      #include <iostream.h>
      main()
      {
      while(!kbhit())
      {
      cout<<"******** *";
      delay(100);///////Notice that the value is in miliseconds
      }
      }[/CODE]
      Last edited by Ganon11; Jan 12 '08, 07:13 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        Voltem’s proposed solution is horribly wrong. Shockingly, each line of code, except for the braces, is questionable. And it’s only 10 lines of code.

        #include <stdlib.h> . Old style header. The new form is <cstdlib> . However, it isn’t necessary here.
        #include <iostream.h> . Old style header. The new form is <iostream> .
        main(). Won’t compile. Only two acceptable forms of main. The one without arguments is int main().
        kbhit. conio.h function. However, conio.h is compiler dependent, and has no standardisation . No good.
        delay(100). Another conio.h function. That’s right, it doesn’t exist in cstdlib. And by relying on conio.h, you rely on a specific compiler.

        Again, the correct way to insert a “pause” is to make the respective thread “sleep”. This is OS dependent. Also, because desktop operating systems are not real time OS, the sleep value is a minimum rather than a precise pause.

        Comment

        Working...