Thread: How can I create a time delay?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Question How can I create a time delay?

    How would one go about creating a delay between the executions of a function? I have made a timer that counts down to zero from a number specified by the user. Unfortunately, it does it all at once. I would like it to pause between printing numbers to the screen.


    Thanks in advance,
    Guideon72

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    try using sleep(number_of_seconds_to_sleep);
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    I wound up with a "call to undefined function in main" compiler error with that. Do I need to include an additional header file for sleep to work?

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    What compiler are you using?
    zen

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    Borland C++ 5.5

  6. #6
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134

    pause

    Hi

    Well i suppose it depends upon what kind of pause you r
    looking for but a for loop usually works , downfall is that when using the same prog on a slower pc the loop takes longer to run through

    GSLR

  7. #7
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    The delay function in DOS.H should provide what you need. Prototype is:

    void delay(unsigned milliseconds);

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    Wheeww!!! Well, I've tried several different ways to get void delay to work and I'm just not getting it. Here is what I have for the prog. so far:

    ____________________code__________________

    #include <stdio.h>
    int main()
    {
    int time;

    printf("How long would you like the timer set for?\n");
    printf("Enter the number of seconds here:");
    scanf(" %d", &time);
    while (0 <= time)
    {
    printf("%d\n", time);
    time--;
    }
    printf("Your food is now burnt!!!\n");
    }

    ____________________end code_________________

    If I put the "delay" in from above, I would need to:
    1) add the dos header (#include <dos.h>)
    2) declare the variable for milliseconds? What is an "unsigned" variable, and do you declare it any differently than a normal one?
    3) insert void delay(milliseconds) into the body of the program:
    a. At the beginning, just after Main?
    b. Just before I want the delay to take place?

    Thanks for all the tips

    Guideon72

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Question Ok...it is that what you want

    May be you are in need of the following system:

    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>

    int main(void)
    {
    int i=0;
    printf ("how much do you want the programme to delay\n");
    scanf("%d",&i);

    printf ("Wait......... for %d MS \n",i);
    delay(i);
    printf("We have returned after a delay!!!\n");

    getch();
    return 0;
    };
    RaHaTk

  10. #10
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    The "unsigned milliseconds" is short for "unsigned int milliseconds", which means that the function accepts an unsigned integer representing the number of milliseconds. You don't need to do anything special to make an integer unsigned, as it will typecast an ordinary integer if necessary - but be careful not to use a negative number. You can declare integers as unsigned in their declarations, though.

    So, just stick any integer variable in the parenthesis or directly put in one in the code:

    Code:
    int duration = 500;
    delay(duration);
    or
    Code:
    unsigned int duration = 500;
    delay(duration);
    or just
    Code:
    delay(100);
    should all work.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    Thanks for the definitions, Procyon! Those were great. I am still getting "undefined function 'delay' in function main()" error when I try to compile this. It does this whether I do #include <dos.> or not.
    I apologize if I'm being needlessly dense, but I want to understand what I'm putting in rather than just parroting someone else's code in my own. I appreciate all the help and suggestions I get here, though.

    Guideon72

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Angry I think.....it is happening for yr compiler

    Okkkkkkk................
    But I think all those are being happend for yr compiler. I think you are not using TURBO C++ compiler. I am using that and haven't faced any problems so far... .
    The prototype for delay() is specified in the Header file named dos.h and after including the file how an error msg may appear!?.
    If your compiler is unable to include the header file then an error msg should appear. Is there any error msg specifying such kinds of error?
    Last edited by rahat; 10-25-2001 at 12:49 PM.
    RaHaTk

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    Rahat,
    I'm using the free Borland C++ 5.5 compiler/linker. The only thing I get is an error message that says, "Call to undefined function 'delay' in function main()". That is all that I get, even with dos.h included.

    I also looked through dos.h and I do not see the prototype for delay in my version. Is it possible that this is an older function that isn't included in newer versions?


    Guideon72

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:

    #include <windows.h>
    .
    .
    Sleep(1000); //Sleep 1 second

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    #include <time.h>




    int pause_sec(int x)
    {
    long T1,T3,T2;
    T1=time(&T2);
    T3=(time(&T2)) + x;
    while(time(&T2) < T3);
    return(x);
    }











    int pause_split_sec(int x)
    {
    long T1,T3,T2;
    T1=clock(&T2);
    T3=(clock(&T2)) + x;
    while(clock(&T2) < T3);
    return(x);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time synchronization problem
    By freeindy in forum C Programming
    Replies: 1
    Last Post: 04-19-2007, 06:25 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM
  4. Time delay
    By Superfrog in forum C++ Programming
    Replies: 5
    Last Post: 09-29-2004, 10:46 AM
  5. Delay Time?? Need As Soon As Possible
    By bigB8210 in forum C Programming
    Replies: 15
    Last Post: 07-15-2003, 04:39 PM