Thread: Countdown timer question:

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Countdown timer question:

    I am having a small problem with a countdown timer program.
    I want it to countdown the time from say 01: 00 : 00 to 00 : 00 :00.
    Here is the program.
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <conio.h>
    
    //A function for the seconds tick.
    void wait( int sec )
    {
         clock_t end_wait;
         end_wait = clock() + sec * CLK_TCK;
         while (clock() < end_wait){}
    }
         
    
    int main(void)
    {
          int hours, seconds, s;
          time_t t;
          struct tm *tm;
          
          tm = localtime(&t);
          printf("Enter the number of seconds: ");
          scanf("%d", &seconds);
          s = seconds;
         
          while(seconds > 0){
          
          tm->tm_sec = s;
          tm->tm_hour = s/3600; 
          tm->tm_min = s/60;
          
          mktime(tm);
    
          printf("%02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
          s--;
          seconds--;
          wait(1);
       
          }
        
        
        getch();
        return 0;
    }
    The problem with the program is that it prints the time every time instead of just updating it like in a digital watch.

    I want the program to print the time: for eg: 00 : 24: 30 and then after a second to print
    00 : 24 :29.. but I want the new time to replace the old one and not print on a new line.

    Any sort of help would be appreciated.
    Regards,
    Vijay

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    replace \n with \r to go back to the beginning of the line instead of the next line in your printf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2

    Thanks A Lot :)

    Awesome.
    Thank you so much.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    you need to use \r to delete what you want. just overwrite them with printf(" ");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  2. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  3. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM