da*********@gma il.com said:
Hi everybody,
I am awfully new to programming in C and all I have had to work with
so far have been tutorials that I've found online. In my searching,
however, I have not found a solution to a problem I have been facing.
Ideally, I would like the program to wait 5 minutes before executing
the rest of the code. Is there some way that I can use time.h and just
run a for loop that waits until 5 minutes has passed to continue down
the program?
There *is* a way you can do that in standard C, yes - but it's not a very
good way. It will work, but it will be a "busy" loop, taking up valuable
processing time. You do it like this:
#include <stddef.h>
#include <time.h>
void delay(unsigned int seconds)
{
time_t start = time(NULL);
double diff = 0.0;
while(diff < seconds)
{
diff = difftime(time(N ULL), start);
}
return;
} /* to delay for five seconds, call delay(5) */
but, like I said, this is not a very good way to do it. Unfortunately, it's
the best way available in standard C. Your C implementation is very likely
to provide a library function that can do basically the same job except
that the processor will be released to do other things. For example, Win32
provides Sleep(), and POSIX(?) provides sleep() and usleep(). If you want
to discuss such functions, I suggest comp.os.ms-windows.program mer.win32
and comp.unix.progr ammer respectively.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999