Need a Time Delay Loop (Turbo c++)

Guys i use turbo c++ 3.0 and borland compiler 5.5.

Can anyone teach me how to insert time delay loops in programs. I heard that these loops delay the execution of the program for a certain time:

like i want a message to be displayed for 5 seconds and then move on to the next message. How do i do this ?

Thanks !!
I'm not sure if i get 100% what your asking, but here something for you to try. you can #include <windows.h> and then type Sleep(5000); the number is in milliseconds
Here you have a portable alternative that uses a lot of CPU:
1
2
3
4
5
6
7
8
9
10
11
void wait(int seconds){
	clock_t endwait;
	endwait=clock()+seconds*CLOCKS_PER_SEC;
	while (clock()<endwait);
}

void waitf(float seconds){
	clock_t endwait;
	endwait=clock()+int(seconds*float(CLOCKS_PER_SEC));
	while (clock()<endwait);
}
Ok thx i will try those out and post back if i have any problem.

Thanks!
Hence you should use not just a simple loop, comaring clock() to endwait, but you should also use sleep in you loop. Even if it's just for a very short time you can free your processor for other executions by using sleep.
It obviously depends in what range you wan't to be able to stop your program, but if you want to wait for something for 5 seconds you can use
1
2
3
4
5
6
void wait(long milliseconds){
 register long endwait;
 endwait = clock() + milliseconds * CLOCKS_PER_SEC * 0.001;
 while (clock() < endwait){sleep(500);} //500ms may be quite inaccurate, but if 
 //it is too inaccurate you can also use sleep(100) or sleep(50).
}
Last edited on
I just used the Sleep() function in windows. h and it works and does the job what i expected.
Can anyone tell me what all other functions are there in this header file ?

Thanks again all.
In the windows.h?
That would be too much to list it here in the forum, because the windows.h includes many other header files (http://en.wikipedia.org/wiki/Windows.h). It includes more or less all standard windows functions, which can be found on http://msdn.microsoft.com/en-us/default.aspx
.
jmc: The whole point of wait() is not to use Sleep() or any other unportable functions. If you're going to use Sleep(), then wait() is unnecessary. Just call Sleep(x) and get it over with.
Last edited on
Topic archived. No new replies allowed.