countdown timer

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 15
Reputation: maye13 is an unknown quantity at this point 
Solved Threads: 0
maye13's Avatar
maye13 maye13 is offline Offline
Newbie Poster

countdown timer

 
1
  #1
Sep 29th, 2007
I need to create a C program that goes like this:

The user wil enter the number of seconds that the stopwatch should run starting at the equivalent time in hours, minutes and seconds of the input. The stopwatch runs until the time becomes 00:00:00.

I don't want you guys to do the whole thing for me, since this is clearly a homework.

Just some hints please?

I've done some experiments but there are still some problems.

Thanks for your time! ^__^

Here's what I've done so far:

  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4.  
  5. void gotoxy();
  6.  
  7. main()
  8. {
  9. int a,b,c,d,e,f,sec,ctr;
  10. a=b=c=d=e=f=ctr=0;
  11. clrscr();
  12. printf("Enter seconds:");
  13. scanf("%d",&sec);
  14. clrscr();
  15. while(ctr<sec){
  16. ctr++;
  17. clrscr();
  18. wherex();
  19. gotoxy(33,12);
  20. printf("%d%d : %d%d : %d%d",f,e,d,c,b,a);
  21. a++;
  22. if(a==10){
  23. b++;
  24. a=0;
  25. }
  26. if(b==6){
  27. c++;
  28. b=0;
  29. }
  30. if(c==10){
  31. d++;
  32. c=0;
  33. }
  34. if(d==6){
  35. e++;
  36. d=0;
  37. }
  38. if(e==10){
  39. f++;
  40. e=0;
  41. }
  42. }
  43. while(ctr>=0){
  44. ctr--;
  45. clrscr();
  46. gotoxy(33,12);
  47. printf("%d%d : %d%d : %d%d",f,e,d,c,b,a);
  48. if(ctr=sec-1){
  49. if(a==0)
  50. a=10;
  51. if(b==0)
  52. b=6;
  53. if(
  54. else if(a==0){
  55. b--;
  56. a=10;
  57. }
  58. else if(b==1){
  59. c--;
  60. b=6;
  61. }
  62. else if(c==1){
  63. d--;
  64. c=10;
  65. }
  66. else if(d==1){
  67. e--;
  68. d=6;
  69. }
  70. else if(e==1){
  71. f--;
  72. e=10;
  73. }
  74. a--;
  75. sleep(1);
  76. }
  77. getch();
  78. }
Last edited by maye13; Sep 29th, 2007 at 6:08 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,513
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: countdown timer

 
0
  #2
Sep 29th, 2007
I don't know why you are doing this the hard way with all those variables. All you need are three variables named Hours, Minutes and Seconds. To print them out with two digits each is like this:
  1. printf("%02d:%02d:%02d\n", Hours, Minutes, Seconds);

After getting keyboard input for the seconds you have to initialize the three variables to the appropriate amount. mktime() function in time.h will help you there. First initialize a struct tm object with all 0s, then set the number of seconds user entered, then call mktime(). After that the struct tm you sent to mktime() will contain the appropriate values of hours, minutes and seconds. Finally you can code a loop to count down the number of seconds originally entered until it reaches 0.
Last edited by Ancient Dragon; Sep 29th, 2007 at 7:22 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,513
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: countdown timer

 
0
  #3
Sep 29th, 2007
After a few minutes testing it turns out to be just a little more complicated then I originally thought, Below is an example of how you need to initialize that structure. First you have to initialize it with current date/time then change the number of seconds to whatever you type in. In the example below I just hardcoded it to a value of 5255 to that mktime() will generate something other than 0 for hours, minutes and seconds.

  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main()
  5. {
  6. time_t today;
  7. struct tm *tm;
  8. today = time(0);
  9. tm = localtime(&today);
  10. tm->tm_sec = 5255;
  11. tm->tm_hour = 0;
  12. tm->tm_min = 0;
  13. mktime(tm);
  14. printf("%02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
  15.  
  16. return 0;
  17. }
Last edited by Ancient Dragon; Sep 29th, 2007 at 7:36 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: maye13 is an unknown quantity at this point 
Solved Threads: 0
maye13's Avatar
maye13 maye13 is offline Offline
Newbie Poster

Re: countdown timer

 
0
  #4
Sep 29th, 2007
I guess I still have to digest all that. ^__^

I'll try to do it again tomorrow.

Thanks!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: countdown timer

 
0
  #5
Sep 29th, 2007
Perhaps more meaningful variable names than a,b,c etc etc would make it easier to follow the code.

Also, it's a good idea to get it working without all the gotoxy() stuff, which is non-portable.

Sure it may go
00:00:10
00:00:09
00:00:08
down the screen, but until it does, all the screen position stuff just gets in the way of solving the core problem.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: maye13 is an unknown quantity at this point 
Solved Threads: 0
maye13's Avatar
maye13 maye13 is offline Offline
Newbie Poster

Re: countdown timer

 
0
  #6
Oct 1st, 2007
Originally Posted by Ancient Dragon View Post
I don't know why you are doing this the hard way with all those variables. All you need are three variables named Hours, Minutes and Seconds. To print them out with two digits each is like this:
  1. printf("%02d:%02d:%02d\n", Hours, Minutes, Seconds);

After getting keyboard input for the seconds you have to initialize the three variables to the appropriate amount. mktime() function in time.h will help you there. First initialize a struct tm object with all 0s, then set the number of seconds user entered, then call mktime(). After that the struct tm you sent to mktime() will contain the appropriate values of hours, minutes and seconds. Finally you can code a loop to count down the number of seconds originally entered until it reaches 0.
There's no mktime() function in time.h. T__T

We're using C v2.01.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: maye13 is an unknown quantity at this point 
Solved Threads: 0
maye13's Avatar
maye13 maye13 is offline Offline
Newbie Poster

Re: countdown timer

 
0
  #7
Oct 1st, 2007
I meant TurboC. ^__^v
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: countdown timer

 
0
  #8
Oct 1st, 2007
> We're using C v2.01.
So I guess Y2K doesn't mean anything to you then?

I simply don't understand why schools keep using these old, obsolete, antiquated tools. You're just going to end up re-learning a bunch of new stuff when you get out into the world and programming with the rest of us.

It's not like there's a shortage of high quality free compilers or anything. There is no excuse for this, other than the tutor being a bone-head and refusing to leave the comfort zone of the compiler they know and love. My guess is such tutors don't know C at all, and they've found that out by trying to use a new compiler and gotten into trouble. So rather than fix the problem (their knowledge of C), they stick with the same-old year after year.
Do we have to wait for these people to retire before the situation changes?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: maye13 is an unknown quantity at this point 
Solved Threads: 0
maye13's Avatar
maye13 maye13 is offline Offline
Newbie Poster

Re: countdown timer

 
0
  #9
Oct 1st, 2007
Okay, this is what I have so far.

  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. main()
  5. {
  6. int hours, minutes, seconds, input;
  7. hours=minutes=seconds=0;
  8. clrscr();
  9. printf("Enter the period that the stopwatch should run (seconds): ");
  10. scanf("%d",&input);
  11. clrscr();
  12. hours=input/3600; input=input-(hours*3600);
  13. minutes=input/60; input=input-(minutes*60);
  14. seconds=input;
  15. gotoxy(33,12);
  16. printf("%02d : %02d : %02d",hours,minutes,seconds);
  17. getch();
  18. }
^
That will just display the number of hours, minutes, and seconds where the countdown will start. I still haven't figured out how to make it run until the time becomes 00:00:00 though.

How I wish we were using something new, really.
Last edited by maye13; Oct 1st, 2007 at 7:11 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: countdown timer

 
0
  #10
Oct 1st, 2007
Say
  1. timeRemaining = input; // use another variable rather than messing with your input
  2. do {
  3. input = timeRemaining;
  4. hours=input/3600; input=input-(hours*3600);
  5. minutes=input/60; input=input-(minutes*60);
  6. seconds=input;
  7. gotoxy(33,12);
  8. printf("%02d : %02d : %02d",hours,minutes,seconds);
  9. timeRemaining--;
  10. // here, delay for 1 second - say delay() in dos.h ?
  11. } while ( timeRemaining > 0 );
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC