Hello, I've been looking arround the internet, and I found the command wait(15); to wait for 15 secons.
I used the header #include <time.h> and I got an error.
Can someone please help me and tell me what I've been doing wrong?

Recommended Answers

All 15 Replies

If you need to wait for an amount of time and if you develop on Windows, use the Sleep function defined in <windows.h>. See this page: http://msdn.microsoft.com/en-us/library/ms686298(v=vs.85).aspx

Note that this approach is not portable on other platforms than Windows.

If ur on windows, use Sleep(milliseconds) located on windows.h. If ur on linux use sleep(seconds) located on unistd.h

I think wait() is an old Turbo C function.

Standards use the sleep(seconds) function, not wait.

Standards use the sleep(seconds) function, not wait.

there are no standards for waiting. *nix, MS-Windows and MS-DOS all use something else. The c++ standards say nothing at all about the topic.

there are no standards for waiting. *nix, MS-Windows and MS-DOS all use something else. The c++ standards say nothing at all about the topic.

Ok, *nix standards use sleep(n). I've also used that with Windoze.

No you haven't. Windows uses Sleep() not sleep() (capital S). Also, *nix sleep() parameter is in seconds -- MS-Windows Sleep() parameter is milliseconds.

commented: Actually answered the question! +1

There's no "wait" command in time.h.

http://www.cplusplus.com/reference/clibrary/ctime/

Don't know where you found this, but it must have either been a user-defined function or another library was included.

/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
  int n;
  printf ("Starting countdown...\n");
  for (n=10; n>0; n--)
  {
    printf ("%d\n",n);
    wait (1);
  }
  printf ("FIRE!!!\n");
  return 0;
}

from cplus.com

.

Vernon was still correct -- there is no wait() in time.h.

No you haven't. Windows uses Sleep() not sleep() (capital S). Also, *nix sleep() parameter is in seconds -- MS-Windows Sleep() parameter is milliseconds.

Ok. It was a long time ago (10+ years), so I probably put an #ifdef in a header to convert sleep(n) to Sleep(n) when it was compiling on a Windows system. This was code that ran on Unix systems as well as Windows, and there was a lot of that sort of cruft to get common code to build on Windows. When it comes to programming standards, there is the POSIX world, and then there is Microsoft's world - ne'er the twain shall meet!

/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

Note that the wait(int) function is user-defined in this example rather than supplied by time.h.

Note that the wait(int) function is user-defined in this example rather than supplied by time.h.

Not necessarily a good approach. This will suck up a huge amount of cpu, a tight loop making a system call like this. In effect, the sleep(n) or Sleep(n) function does a kernel call to set an interval timer and wakes up when the time expires. Because this is handled in the kernel idle loop, without recourse to a user loop, it effectively takes no CPU time at all.

Not necessarily a good approach. This will suck up a huge amount of cpu, a tight loop making a system call like this.

Agreed. I don't think anyone was actually recommending that solution. I think it was on cplusplus.com's site since it was a good example of clock(), not a good example of how to pause. Overall point is that pausing appears to be OS-specific.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.