Thread: Using the sleep function with a float

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User keyboard's Avatar
    Join Date
    Nov 2011
    Posts
    8

    Using the sleep function with a float

    I have written this code and it will work with an interger but not a float. I have tried casting but it does not run correctly. I want to pause for 0.5 seconds when each line prints. Here is the code:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main() {
    
        int n = 1;
        char fz[] = "Fizz";
        char bz[] = "Buzz";
        char fb[] = "FizzBuzz";
        float sec = 0.5;
    
        for ( n = 1; n < 101 ; ++n ) 
            if ( n % 15 == 0 ) {
                printf("%s\n", fb);
                sleep((int)sec);
            } else if ( n % 5 == 0 ) {
                printf("%s\n", bz);
                sleep((int)sec);
            } else if ( n % 3 == 0 ) {
                printf("%s\n", fz);
                sleep((int)sec);
            } else {
                printf("%d\n", n);
                sleep((int)sec);
            }
    
        return 0;
    
    }
    If I change the function line to sleep(1); it works but is a bit slow.
    Last edited by keyboard; 11-15-2011 at 10:32 AM. Reason: errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep function
    By pollypocket4eva in forum C Programming
    Replies: 11
    Last Post: 02-16-2009, 04:28 AM
  2. How to use Sleep function?
    By Jake.c in forum C Programming
    Replies: 1
    Last Post: 01-17-2009, 08:36 AM
  3. Using the sleep function
    By HyperHelix in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2005, 09:18 AM
  4. The sleep function...
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2004, 03:19 PM
  5. sleep function
    By subflood in forum C Programming
    Replies: 1
    Last Post: 08-29-2004, 03:11 PM