Thread: problem with countdown timer

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    47

    Question problem with countdown timer

    Hey guys. I just created a word game with a countdown timer but the problem is the timer wont start automatically. You have to press any button for to start. Can you please help me with these? Here's my code for the timer.

    Code:
    #include<stdio.h>
    #include<time.h>
    #include<conio.h>
    void wait (int sec)
    {
    clock_t end_wait;
    end_wait=clock() + sec  *CLK_TCK;
    while (clock()<end_wait){}
    }
    
    int main (void)
    {
    int  hours, seconds=10, s;
    time_t t;
    struct tm *tm;
    
    tm=localtime(&t);
    s=seconds;
    
    while(seconds>=0){
    tm->tm_sec=s;
    tm->tm_hour=s/3600;
    tm->tm_min=s/60;
    
    mktime(tm);
    
    printf("%02d:%02d\n", tm->tm_min, tm->tm_sec);
    s--;
    seconds--;
    wait(1);
    
    if (tm->tm_sec==0)
    {
    printf("GAME OVER. PLEASE TRY AGAIN");
    }
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Some problems could be found by indenting your code. For example, it seems you have "return 0;" too early (you have it inside your loop, so your game only counts one second).

    Another problem is clock() is for measuring CPU time, not wall clock time. To wait for a number of seconds, maybe you want to do something like the following:

    Code:
    #include <time.h>
    #include <unistd.h>
    
    void wait_seconds(int sec)
    {
        time_t t_now = time(NULL);
        time_t t_end = t_now + (time_t)sec;
        while (t_now <= t_end) {
            t_now = time(NULL);
            usleep(1000); /* avoid hogging cpu */
        }
    }

    usleep is OS specific but most environments have this or something similar. There is also normally a sleep function which sleeps for 1 second. However, doing it in this way gives you the chance to do some work while "waiting", which is probably what you will ultimately end up doing in a game.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Does your use of conio.h indicate you're using TurboC, or some other DOS-era compiler?

    Second, you need to learn about indentation. Pick a style you like and stick to it. An indent style of "left" isn't going to work out for you, nor anybody that would care to help you.

    Third, in order to implement what you ask, you need some kind of non-blocking I/O, which in turn means you need to tell us which OS/Compiler you're using. Standard C doesn't do non-blocking input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. want to add countdown timer to my program
    By megazero1316 in forum C Programming
    Replies: 1
    Last Post: 12-14-2013, 01:00 PM
  2. Countdown timer question:
    By Vadurr in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 08:13 AM
  3. Countdown timer
    By george7378 in forum C++ Programming
    Replies: 1
    Last Post: 06-24-2010, 05:29 AM
  4. timer problem
    By robin171 in forum C Programming
    Replies: 2
    Last Post: 10-17-2005, 08:48 AM
  5. Replies: 4
    Last Post: 08-13-2003, 07:25 PM