Dec 15, 2011

Function delay () in C++


A function is sometimes useful sometimes not much is the delay (), used to make a pause 'n' seconds.In some Borland compilers this function is included in the conio.h library but here are the free software side we will do ourselves this function. First let's see how it is used:
delay(variable_tipo_int);
ie:
int secs=10;
delay(secs);

or
delay(numero_entero);
ie:
delay(12);
And of course, the function is this:
void delay(int secs) {
for(int i = (time(NULL) + secs); time(NULL) != i; time(NULL));
}
  1. The function time (NULL) returns us to the exact second we are.
  2. If we add a certain amount of seconds we get a date in the future.
  3. At each turn of the cycle checks if the date in the future is different from the current date.
  4. As the two dates are equal, the cycle ends and the program continues.
Let's see how it is used in a program in c + +:
 # Include <time.h>
 # Include <iostream>
 using namespace std;

 void delay (int secs) {
   for (int i = (time (NULL) + secs) time (NULL)! = i; time (NULL));
 }

 int main ()
 {
     int sec;
     court <<"Seconds to pause?:" cin>> sec;
     delay (sec);
 } 
To use the delay function in this way is necessary to put in all programs where we need to use it (now I realize I have not written much about functions) and always include the library # include <time.h> or# include <ctime >.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes