Set delay time between printf() statements

This is a discussion on Set delay time between printf() statements within the C Programming forums, part of the General Programming Boards category; Hello, I was wondering if it was possible to set, for example, a 3 second delay between printf() statements? Example: ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Set delay time between printf() statements

    Hello,

    I was wondering if it was possible to set, for example, a 3 second delay between printf() statements?

    Example:

    Please enter your age: 35

    >3 second delay<

    Please enter your gender: M

    >3 second delay<

    You're a 35 year old male.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Sure. Just use the 'clock' function to count off the time.

    EDIT: Don't forget to normalize the results with CLK_TCK.
    Last edited by Sebastiani; 03-23-2010 at 01:50 PM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Thanks, I'll look into it.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Or you could use Sleep() or sleep() (depending on your system) so you don't have to burn so many cycles.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Why would you do that?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    It's for a practical joke (under normal circumstances, I wouldn't imagine a reason for it either).

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by SilentPirate007 View Post
    It's for a practical joke (under normal circumstances, I wouldn't imagine a reason for it either).
    I think he was referring to NeonBlack's post, actually.

    Anyway, once you have that function worked out, try this:

    Code:
    void pause( clock_t val )
    {
    /*
        ...the implementation...
    */
    }   
        
    clock_t next( )
    {
        static int
            unset = 1;
        if( unset )
        {
            --unset;
            srand( time( 0 ) );
        }
        return ( ( clock_t )( rand( ) * 1000 * ( 1 / ( double )RAND_MAX ) ) ) >> 2;
    }
            
    void say( char const* str )
    {
        while( *str )
        {
            putchar( *str++ );
            pause( next( ) );
        }
    }
    
    int main( void ) 
    {
        say( "I think, therefore I am." );
    }

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Right,

    well, I found that the quickest and shortest way was to use NeonBlacks' method.
    I added <windows.h> and beneath my statement, I put: Sleep(3000);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck with function
    By scmurphy64 in forum C Programming
    Replies: 9
    Last Post: 11-10-2009, 10:41 AM
  2. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  4. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 02:44 AM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21