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:

#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();
}
Ancient Dragon commented: Thanks for using code tags +19

Recommended Answers

All 10 Replies

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 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.

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;
}

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

I'll try to do it again tomorrow.

Thanks!!

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.

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 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.

I meant TurboC. ^__^v

> 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?

Okay, this is what I have so far.

#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.

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 );

Shucks! Why DIDN'T I think of that.

Thanks a lot! ^__^

commented: Good luck :) +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.