Like Tree2Likes
  • 1 Post By Tibo-88
  • 1 Post By adeyblue

Using the sleep function with a float

This is a discussion on Using the sleep function with a float within the C Programming forums, part of the General Programming Boards category; I have written this code and it will work with an interger but not a float. I have tried casting ...

  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

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    I suggest using more than a single line of code inside the code tags.

    Tim S.

  3. #3
    Registered User keyboard's Avatar
    Join Date
    Nov 2011
    Posts
    8
    It was a mistake to post it that way. I'll try to fix it.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    You could take a look at the usleep() function : usleep(3) - Linux man page
    Salem likes this.

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    I don't think the majority will be able to help you with that program. It's simply far too advanced for those of us who are Comp Sci. graduates

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    What result do you expect (int)sec to give?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

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, 10:18 AM
  4. The sleep function...
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2004, 04:19 PM
  5. sleep function
    By subflood in forum C Programming
    Replies: 1
    Last Post: 08-29-2004, 04:11 PM

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