# waiting a fraction of a second (C++)..



## daveyp225 (May 13, 2003)

Is there any built in function to c++ (specifically the borland compiler) that allows for a program to stop and wait a certain amount of seconds before continuing on?
I made my own function that does that, but it only works with whole seconds, and not fractions of seconds. Ive been trying to do this on my own for the last two days but haven't gotten very far. I want the program to look like its typing by itself and a whole second between keystrokes seems a BIT long..

Also.. I need to let the program to continue other events while waiting, since this kind of loop temporarily freeezes the program.. Kind of like DoEvents() in basic.
I tried the sleep() function but it says undefined function for some reason, and yes dos.h is included..

If you can help, please do!
Thanks


----------



## daveyp225 (May 13, 2003)

Here is an example of what I want to do:
This just outputs the lowercase alphabet, but takes almost a second between each letter instead of the passed 0.1 seconds..

(This is the actual timeout function I made)

```
void timeout(double seconds)
{
time_t timer1, timer2;
timer1 = time(NULL);
while (timer2 - timer1 <= seconds) {
timer2 = time(NULL);}

void main(){
for (int a=97; a<=122; a++)
{
cout << char(a);
timeout(0.1);
}
}
```
I figured seeing the code might help...


----------



## AlbertB (Nov 24, 2002)

Try this:


```
clock_t timeout(clock_t timeIntoFunc, double increment)
{
   clock_t timeOutFunc ;

   while ((clock() - timeIntoFunc) < increment)
   {
      timeOutFunc = time(NULL);
   }
   return timeOutFunc;
};

void main()
{
   clock_t timeOutFunc ;

   for (int a=97; a<=122; a++)
   {
      clock_t timeIntoFunc = clock();
      timeOutFunc = timeout(timeIntoFunc, 200.0);
      cout << char(a) << "    " << timeOutFunc << endl;
   }
}
```
I really don't know much about this field at all but I have used a variable of type "clock_t", timeIntoFunc, to mark the time we go into the timeout() function, which incidentally now returns a value of type clock_t also. We pass the value of that time in along with the required delay *in milliseconds*.

The While loop is now controlled by comparing the difference between the current time and the time we entered, with the required increment, and exiting when we pass that point.

The function returns a value of the time of exit, which we now report along with our alphabet character. This was just to get a rough check on the number of repetitions per second. You will see with 200.0, there are 5 letters printed per count of timeOutFunc, ie 200milliseconds per letter.

It runs using MS Dev Studio 6 here and the time is constantly variable by altering the value currently set at 200.0.

It is rough and ready but you should be able to tidy it up and hopefully get some useful ideas from it.


----------



## daveyp225 (May 13, 2003)

Wow, thanks, it works just how I wanted it to!

Now I still need to allow other events to be able to be executed because when the loop is entered the program "freezes" until it exits from it. I just dont want someone to not be able to move the program box around while it's "typing"..
For anyone that is familiar with Visual Basic.. the DoEvents() function is basically what I need an equivilant of..

thanks again albert.


----------

