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:
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 astruct 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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
#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;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> 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?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Say
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 );
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953