Thread: Delay

  1. #1
    Unregistered
    Guest

    Delay

    Can someone tell me what would be the best way to create a delay in my program of about 2 seconds.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    - Buy a slower PC.
    - Search the board for answers, this has been asked many times.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    /*
    
        Function:
        wait_a_moment(int seconds)
        
        Purpose:
        Provide a predetermined pause in the program
        giving the user enough time to read a message.
        Continue program execution after time is up
        without any user interaction.
        
        Usage:
        wait_a_moment(3) - Pauses for 3 seconds
        wait_a_moment(5) - Pauses for 5 seconds
        
    */
    
    #include <time.h>
    
    void wait_a_moment(int seconds);
    
    void wait_a_moment(int seconds) 
    {
        clock_t endtime = clock() + seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
    }
    The world is waiting. I must leave you now.

  4. #4
    Unregistered
    Guest
    shadow,
    I appreciate your help
    I have already tried using that code and I get a bunch of errors
    what could I possibly be leaving out
    Here is what I have:
    #include <stdio.h>
    #include <tjpbase.h>
    #include <time.h>

    using namespace std;



    void main()
    {
    init_motortjp();

    motorp (0,50);
    motorp (1,50);

    void wait_a_moment(int seconds);

    void wait_a_moment(int seconds)
    {
    clock_t endtime = clock() +seconds * CLOCKS_PER_SEC;
    while ( ( clock() < endtime ) );
    }
    and the errors:
    !E straight.c(20): syntax error; found `seconds' expecting `;'
    !E straight.c(20): warning: expression with no effect elided
    !E straight.c(20): syntax error; found `)' expecting `;'
    !E straight.c(20): illegal statement termination
    !E straight.c(20): skipping `)'
    !E straight.c(22): undeclared identifier `clock_t'
    !E straight.c(22): warning: expression with no effect elided
    !E straight.c(22): syntax error; found `endtime' expecting `;'
    !E straight.c(22): too many errors

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    void main()
    {
    init_motortjp();

    motorp (0,50);
    motorp (1,50);
    Your main function doesn't return a value.
    You used void main. Don't.
    Your main function doesn't have a closing brace.
    You put your secondary function's prototype inbetween your main function, and the function itself.

    The following program demonstrates how to do all of these things, and use the code I submitted.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    void wait_a_moment(int seconds);
    
    int main(void)
    {
            printf("Pausing for 3 seconds..");
            wait_a_moment(3);
            printf("Pausing for 5 seconds..");
            wait_a_moment(5);
            return 0;
    }               
    
    void wait_a_moment(int seconds) 
    {
        clock_t endtime = clock() + seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
    }
    [EDIT]
    Compile this code as is.
    [/EDIT]
    The world is waiting. I must leave you now.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >using namespace std;
    What language are you writing in..? Make your mind up, is it C or C++?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding delay in the connection
    By byatin in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-19-2008, 02:59 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-05-2005, 11:23 AM
  4. 2-3 Second delay with alarm()
    By Longie in forum C Programming
    Replies: 11
    Last Post: 06-20-2004, 08:46 PM
  5. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:28 AM