/* ---------------------------------------------------------------------- // Figure 6.19: Using a delay loop. // Figure 6.20: Delaying progress, the delay() function. // ---------------------------------------------------------------------- // Note: The delay function is defined in the tools library, which is // included here. A copy of the code is given below, enclosed in a // comment so that it will not interfere with compilation and linking. */ #include "tools.h" void main ( void ) { int j, max, seconds; printf( "This is an exercise program.\n\n" "How many pushups are you going to do? " ); scanf( "%i", &max); if (max < 0) fatal( "Can't do %i pushups!", max ); printf( "How many seconds between pushups? " ); scanf( "%i", &seconds ); if (seconds < 3) fatal( "Can't do a pushup in %i seconds!", seconds ); printf( "OK, we will do %i pushups, one every %i seconds.\n" "Do one pushup each time you hear the beep.\n", max, seconds ); for (j = 1; j <= max; ++j) { printf( "%i \a\n", j ); /* Do one. */ delay( seconds ); /* Now wait specified # of seconds. */ } puts( "Good job. Come again." ); } /* -------------------------------------------------------------------------- */ // Delay progress of program for some number of seconds using a "busy wait". #include void delay( int seconds ) { time_t goal = time( NULL ) + seconds; do { /* Nothing */ } while (time( NULL ) < goal); }