Countdown timer question:

This is a discussion on Countdown timer question: within the C Programming forums, part of the General Programming Boards category; I am having a small problem with a countdown timer program. I want it to countdown the time from say ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    replace \n with \r to go back to the beginning of the line instead of the next line in your printf
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 01:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

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