Like Tree1Likes
  • 1 Post By grumpy

Help with running a function every second.

This is a discussion on Help with running a function every second. within the C Programming forums, part of the General Programming Boards category; Hello Everyone, This is my first post so sorry if it's bad. Here is my code: Code: #include <stdio.h>#include <unistd.h> ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    16

    Help with running a function every second.

    Hello Everyone,
    This is my first post so sorry if it's bad.

    Here is my code:
    Code:
    #include <stdio.h>#include <unistd.h>
    
    
    
    
    int main()
    {
       int age;
       printf( "How old are you?" );
       scanf( "%d", &age );
       if ( age < 80 ) {
            printf( "Many years of learning ahead\n\n");
            }
        else {
            printf( "May the force be with you\n\n");
            }
    
    
    system("PAUSE\n\n");
    printf("Lets play a game.\n\n");
    printf("You have %d seconds to cancel your computer from being shutdown.\n", age);
    printf("Good luck.\n\n");
    do
    {
        printf("%d\n",age);
        sleep(1);
        age-=1;
    } while(age>=0);
    system("pause");
    return 0;
    }
    Obviously the computer isn't going to be shutdown(it's to scare my brother) but every time I try to compile it, it says undefined reference to 'sleep'.
    So what I want to happen is that it countsdown every second from their age.
    Any help is appreciated. Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Lemme guess. You are compiling under windows.

    Replace the "#include <unistd.h>" line with
    Code:
    #ifdef __unix__
    # include <unistd.h>
    #elif defined _WIN32
    # include <windows.h>
    #define sleep(x) Sleep(1000 * x)
    #endif
    If this doesn't do it, you need to read the documentation with your compiler and development environment.

    Bear in mind that both sleep() and Sleep() are not standard C functions.

    It would also be a good idea to replace the system() calls with something else.

    As to the games to scare your brother ..... I suggest you find something more worthwhile to do.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    If your compiler doesn't have the sleep function, you can just make a for() loop that does nothing for 450000000 iterations, and then do a count down from age to zero by enclosing the for() loop in a do...while() loop.

    If your computer is super quick, then you might need number larger than 450000000(or smaller number if your computer is slow) - play around with the number so that it coresponds to one second.

    Code:
     
    #include <stdio.h>
    
    int main(void)
    {
        int age = 13;
        int age_ctr;
        int ctr;
    
        do
        {
            for(ctr = 0; ctr < 450000000; ctr++)
            {
                ;
            }
            printf("%d\n", age);
            age--;
        } while(age > 0);
        return 0;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by cfanatic View Post
    Code:
     
            for(ctr = 0; ctr < 450000000; ctr++)
            {
                ;
            }
    }
    There are two problems with this. Firstly, you are hardcoding an assumed processor speed.

    Second, plenty of real-world compilers are smart enough to simply optimise that loop away (i.e. eliminate it completely from the executable), since it has not effect on program output. I know of at least one compiler that WOULD eliminate that loop even with all optimisation settings disabled.
    Salem likes this.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Sorry for a late reply but thank you all for your responses.
    To grumpy- Thank you for your response for it worked for me.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by grumpy View Post
    Second, plenty of real-world compilers are smart enough to simply optimise that loop away (i.e. eliminate it completely from the executable), since it has not effect on program output. I know of at least one compiler that WOULD eliminate that loop even with all optimisation settings disabled.
    Just as a matter of interest, which compilers would eliminate this loop? I have compiled it in MingW(Windows port of GCC), with no optimizations, and the loop is compiled and the executable runs the loop.

    Can someone with VC++ or some other compiler run this code and give us feedback? Thanks.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program freezing while running a function, please help
    By kuzadaman in forum C Programming
    Replies: 6
    Last Post: 07-25-2011, 07:05 PM
  2. Running time function in c
    By lio in forum C Programming
    Replies: 2
    Last Post: 01-21-2011, 05:27 AM
  3. Function not running yet runtime error???
    By scwizzo in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2008, 11:35 AM
  4. Invoke a function from another running program (EXE) in C/C++
    By sureshobabu in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2006, 06:32 AM
  5. How to add data and function as the program is running
    By hOOkwOrM in forum C++ Programming
    Replies: 9
    Last Post: 08-23-2003, 05:27 PM

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