This is my code and i do understand the logic but i don't understand the algorithm. About the Parking time coz when i give 1:26 in the time in and 2:20 in time out, i got a negative parking time. here's my code

#include<stdio.h>

void menu(char a);
void getData(int *a, int *b, int *c, int *d);
void dis(int a, int b, int c, int d);
int hrsT(int a, int b);
int minT(int c, int d);
void res(int hr, int min);

int main()
{
	int a,b,c,d;
	char e;

	clrscr();
	printf("Type of vehicle: ");
	scanf("%c", &e);
	menu(e);
	getch();

}

void menu(char a)
{
	int l,m,n,o;

	switch(a)
	{
		case 'C':
		case 'c': clrscr();
			  printf("\t\tCar\n\n");
			  getData(&l,&m,&n,&o);
			  dis(l,m,n,o);
			  res(hrsT(l,n),minT(m,o));
			  break;

		case 'B':
		case 'b': clrscr();
			  printf("\t\tBus\n\n");
			  getData(&l,&m,&n,&o);
			  break;

		case 'T':
		case 't': clrscr();
			  printf("\t\tTruck\n\n");
			  getData(&l,&m,&n,&o);
			  break;
	}
}

void getData(int *a, int *b, int *c, int *d)
{
	printf("Hour vehicle entered a lot (0-24)?: ");
	scanf("%d", a);
	printf("Minute vehicle entered a lot (0-60)?: ");
	scanf("%d", b);
	printf("Hour vehicle left a lot (0-24)?: ");
	scanf("%d", c);
	printf("Minute vehicle left a lot (0-60)?: ");
	scanf("%d", d);
}

void dis(int a, int b, int c, int d)
{
	printf("\n\nTime In\t\t  %d : %d", a,b);
	printf("\nTime Out\t  %d : %d", c,d);
	printf("\n\t\t -------");
}

int hrsT(int a, int b)
{
	int hrsT;

	if(b<a)
		hrsT=(a-b)-1;
	else
	hrsT=b-a;
	return hrsT;
}

int minT(int c, int d)
{
	int minT;

	if(d<c)
		minT=(c-d)-60;
	else
	minT=d-c;

	return minT;
}

void res(int hr, int min)
{
	int time;

	printf("\nParking time\t  %d : %d", hr, min);
	if(min>0 || min<0)
		time=hr+1;
	printf("\nRounded total\t      %d", time);
}

Could someone clarify this things?

Recommended Answers

All 8 Replies

Change your logic to:
if (out_minute < in_minute)
then minute = 60 - (in_minute - out_minute)
and hour = hour - 1;

You can better have one single function call for your time calculation, rather than calculating minute and hour separately...

kings_mitra is not wrong, but it is easier in my opinion to calculate start and end time as (minutes or seconds) since some past moment (like, in this case, midnight, or if the interval can include midnight, then perhaps midnight yesterday or midnight on the 1st of January 1970 (for which there are library routines)

Well, since it is a car parking program, year is never going to come into picture.
For, seconds(60), minutes(60), hours(12 or 24), days(28, 30 or 31) and months(12) we should always have that check if we are calculating time differences.

It depends on the application and in this case he is happy with only hours and minutes (though day count is also required, I suppose). But checking for seconds, again won't make any sense for this application...

Story: I park the car near the bar on Dec 31st in the evening. After celebrating the new year in, I decide to take the cab home. After I recover all day on the 1st, I come back on the morning of the 2nd to get the car. For that you would need to deal with years, months, days and hours with the months days and hours apparently backward until you take everything into account.

It does depend on what the prof or shirmaster really wants, but mktime and difftime are thoroughly debugged and functional. Chant man 3 mktime to see (on my BSD-ish Mac at least) more than you ever wanted to know about such things.

I have hour in and hour out, minute in and minute out. Seconds doesn't include here or even days or months for the time is measured in 24 hour.

I didn't got the idea of mr kings_mitra for the hour there doesn't describe hour in or out. I do understand that the hour and minute must be in 1 function. Need help again :(

Mr kings_mitra code is the same as mine only i had a different function on it. I need much help here badly :(. Hours and minutes only involved in this and 24 hour format.

griswolf,
Yeah, you are correct... I missed that point.
In that case year also has to be taken care.

Using the standard library routines will definitely make it easy for the program construct.

shirmaster,

If you are concerned only about the hour and minute for one single day(24 hours only) then my first post is enough for your solution.
Since, you have not got the idea I will explain with your own example :
Intime : 1:26
Outime : 2:20

So, for hour calc,
since (out_hr > in_hr)
hour = 2 - 1 = 1
For minute calc,
since (out_min < in_min)
minute = 60 - (26 - 20) = 54
and hour = hour - 1 = 1 - 1 = 0

So, time difference is 0 hours and 54 mins.

I think this will clear up your doubts...

Mr kings_mitra code is the same as mine only i had a different function on it.

Is it so... Check it again, man...!!!

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.