Thread: want to add countdown timer to my program

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    want to add countdown timer to my program

    hi i want to add a countdown timer to my program so after specific time it go back to the main menu my program is something like vending machine

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I don't think there's a standard way to do this. I would look into using an event library that can generate an event at a specific time. Then you can write things that need to happen as event handlers, such as your timeout.

    If you want to perform your own event handling or if your needs are simple you can create a simple "forever" loop that checks for the various events you are interested in like keyboard and timeouts and then reacts to them accordingly.

    Code:
    for (;;) {
        // check for and handle keyboard presses
        // TODO
    
        // check the current time to see if a timeout has occured
        // TODO
    
        // redraw or update the screen
        // TODO
    
         // yield some processor time to the operating system
        usleep(100);
    }
    To check for keyboard presses you can use an OS-specific method, as there is no standard method for this. I would recommend curses to keep your code portable.

    For updating the screen again probably an OS-specific method is in order.

    For checking the time there is gettimeofday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Countdown timer question:
    By Vadurr in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 08:13 AM
  2. Countdown timer
    By george7378 in forum C++ Programming
    Replies: 1
    Last Post: 06-24-2010, 05:29 AM
  3. Countdown in Program
    By peckitt99 in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2006, 07:39 AM
  4. Replies: 4
    Last Post: 08-13-2003, 07:25 PM
  5. Whats wrong with my countdown program?
    By Golden Bunny in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2002, 01:15 PM