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?

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

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.

Comments
Actually answered the question!

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

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.

This article has been dead for over six months. Start a new discussion instead.