| | |
countdown timer
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
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:
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void gotoxy(); main() { int a,b,c,d,e,f,sec,ctr; a=b=c=d=e=f=ctr=0; clrscr(); printf("Enter seconds:"); scanf("%d",&sec); clrscr(); while(ctr<sec){ ctr++; clrscr(); wherex(); gotoxy(33,12); printf("%d%d : %d%d : %d%d",f,e,d,c,b,a); a++; if(a==10){ b++; a=0; } if(b==6){ c++; b=0; } if(c==10){ d++; c=0; } if(d==6){ e++; d=0; } if(e==10){ f++; e=0; } } while(ctr>=0){ ctr--; clrscr(); gotoxy(33,12); printf("%d%d : %d%d : %d%d",f,e,d,c,b,a); if(ctr=sec-1){ if(a==0) a=10; if(b==0) b=6; if( else if(a==0){ b--; a=10; } else if(b==1){ c--; b=6; } else if(c==1){ d--; c=10; } else if(d==1){ e--; d=6; } else if(e==1){ f--; e=10; } a--; sleep(1); } getch(); }
Last edited by maye13; Sep 29th, 2007 at 6:08 am.
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:
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.
C Syntax (Toggle Plain Text)
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.
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <time.h> int main() { time_t today; struct tm *tm; today = time(0); tm = localtime(&today); tm->tm_sec = 5255; tm->tm_hour = 0; tm->tm_min = 0; mktime(tm); printf("%02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec); return 0; }
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.
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.
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.
•
•
•
•
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:
C Syntax (Toggle Plain Text)
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.
We're using C v2.01.
> 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?
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?
Okay, this is what I have so far.
^
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.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> main() { int hours, minutes, seconds, input; hours=minutes=seconds=0; clrscr(); printf("Enter the period that the stopwatch should run (seconds): "); scanf("%d",&input); clrscr(); hours=input/3600; input=input-(hours*3600); minutes=input/60; input=input-(minutes*60); seconds=input; gotoxy(33,12); printf("%02d : %02d : %02d",hours,minutes,seconds); getch(); }
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.
Say
C Syntax (Toggle Plain Text)
timeRemaining = input; // use another variable rather than messing with your input do { input = timeRemaining; hours=input/3600; input=input-(hours*3600); minutes=input/60; input=input-(minutes*60); seconds=input; gotoxy(33,12); printf("%02d : %02d : %02d",hours,minutes,seconds); timeRemaining--; // here, delay for 1 second - say delay() in dos.h ? } while ( timeRemaining > 0 );
![]() |
Similar Threads
- timer in VB6 (Visual Basic 4 / 5 / 6)
- Tkinter Countdown Timer problems. (Python)
- Pause/Restart Countdown Timer Question (VB.NET)
- Countdown Timer (VB.NET)
- timer countdown help (Visual Basic 4 / 5 / 6)
- Timer Countdown (C++)
- Countdown (Java)
Other Threads in the C Forum
- Previous Thread: Reading RM/RMVB
- Next Thread: Removing duplicates
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






