943,545 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 10740
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 29th, 2007
1

countdown timer

Expand Post »
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.
Similar Threads
Reputation Points: 40
Solved Threads: 1
Newbie Poster
maye13 is offline Offline
18 posts
since Sep 2007
Sep 29th, 2007
0

Re: countdown timer

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Sep 29th, 2007
0

Re: countdown timer

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Sep 29th, 2007
0

Re: countdown timer

I guess I still have to digest all that. ^__^

I'll try to do it again tomorrow.

Thanks!!
Reputation Points: 40
Solved Threads: 1
Newbie Poster
maye13 is offline Offline
18 posts
since Sep 2007
Sep 29th, 2007
0

Re: countdown timer

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 1st, 2007
0

Re: countdown timer

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.
Reputation Points: 40
Solved Threads: 1
Newbie Poster
maye13 is offline Offline
18 posts
since Sep 2007
Oct 1st, 2007
0

Re: countdown timer

I meant TurboC. ^__^v
Reputation Points: 40
Solved Threads: 1
Newbie Poster
maye13 is offline Offline
18 posts
since Sep 2007
Oct 1st, 2007
0

Re: countdown timer

> 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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 1st, 2007
0

Re: countdown timer

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.
Reputation Points: 40
Solved Threads: 1
Newbie Poster
maye13 is offline Offline
18 posts
since Sep 2007
Oct 1st, 2007
0

Re: countdown timer

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 );
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Reading RM/RMVB
Next Thread in C Forum Timeline: Removing duplicates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC