# calculating garage charges

• 02-22-2009
nynicue
calculating garage charges
Hi, I'm trying to make a program to calculate garage charges, and having a trouble with calculating overnight charges. When time entered between 4PM and 6AM and exit by 8AM a charge must be \$11, but my program gives me wrong output. Please help, can't understand what mistake is.

Here is a function to calculate charges

Code:

```int calculate_charge(int x) {         if(((16<=hour_in)&&(hour_in<6))&&(hour_out<=8))         {                 charge=11;         }         else         {                 if(x>10&&x<=24)                         charge=27;                 else if(x>=0&&x<1)                         charge=8;                 else if(x<2)                         charge=12;                 else if(x<3)                         charge=16;                 else                         charge=22;                         }         //printf("\n%i",charge);         return charge; }```
And this the whole program.

Code:

```        #include<stdio.h> int hour_in, min_in, hour_out, min_out; int daytime, eveningtime, hour,total_hour; int charge, amount_collected; void get_time_in_and_out(void) {         int AM_PM;         printf("Enter time in\n");//enter time in         printf("\tHour:");         scanf("%i",&hour_in);                         printf("\n\tMinuts:");         scanf("%i",&min_in);         printf("\n0 - AM ; 1 - PM\t");         scanf("%i",&AM_PM);                                         if(AM_PM==1&&hour_in<12)                 {                         hour_in+=12;                 }                 else if(AM_PM==0&&hour_in==12)                 {                         hour_in+=12;                 }                 else                 {                         hour_in+=0;                 }         printf("\nEnter time out\n");//enter time out         printf("\tHour:");         scanf("%i",&hour_out);         printf("\n\tMinuts:");         scanf("%i",&min_out);                         printf("\n0 - AM ; 1 - PM\t");         scanf("%i",&AM_PM);                                         if(AM_PM==1&&hour_out<12)                         hour_out+=12;                 else if(AM_PM==0&&hour_out==12)                         hour_out+=12;                 else                         hour_out+=0;                 } int calculate_hours(void) {         int total;                 total=(((hour_out*60)+min_out)-((hour_in*60)+min_in));         hour=total/60;                 return hour; } int calculate_charge(int x) {         if(((16<=hour_in)&&(hour_in<6))&&(hour_out<=8))         {                 charge=11;         }         else         {                 if(x>10&&x<=24)                         charge=27;                 else if(x>=0&&x<1)                         charge=8;                 else if(x<2)                         charge=12;                 else if(x<3)                         charge=16;                 else                         charge=22;                         }         //printf("\n%i",charge);         return charge; } void main(void) {         int Done=1;         int car=1;         int amount_collected=0;         total_hour=0;         do         {         printf("Car#%i:\n\n", car);         get_time_in_and_out();                 calculate_hours();                                 total_hour+=hour;         calculate_charge(hour);                 amount_collected+=charge;         ++car;         printf("===============================");         printf("\n1 -Next car\n0 -Done  ");         scanf("%i",&Done);         printf("===============================\n\n");         }while(Done==1);         printf("Tota Hours Parked:\t %i\n",total_hour);         printf("Amount Collected:\t%i\n",amount_collected);         }```
• 02-22-2009
Think about what hour_in time(s) are less than 16, and what hour_in times are greater than 16.
• 02-22-2009
tabstop
Also as a hint, what does your calculate_hours return in this case? Is it right?
• 02-22-2009
nynicue
Thank you, I think I got it , will try to fix
• 02-22-2009
nynicue
No, still did not get it
Quote:

Think about what hour_in time(s) are less than 16, and what hour_in times are greater than 16.
I don't compare it to the amount of hours, for overnight charges: I need so when car enters between 16:00 (evening)and 4:00(morning next day) and exit by 8AM be charges with &11.

Quote:

Also as a hint, what does your calculate_hours return in this case? Is it right?
this function return amount of hours, I need it for calculating Daytime charges and also for calculating total amount of hours at the end of the day.

I tried to do this, but it did not work also:
Code:

```int calculate_charge(int x) {         if((((hour_in>=16)&&(hour_in<=24))||((hour_in>=0)&&(hour_in<6)))&&(hour_out<=8))         {                 charge=11;         }         else         {                 if(x>10&&x<=24)                         charge=27;                 else if(x>=0&&x<1)                         charge=8;                 else if(x<2)                         charge=12;                 else if(x<3)                         charge=16;                 else                         charge=22;                         }         //printf("\n%i",charge);         return charge; }```
• 02-22-2009
tabstop
It would probably be easier to fix your calculate_hours then do nine million cases in your charges function, especially when you don't pass in the times of day into your charge function, but just the number of hours parked, and especially when you have to total the number of hours parked. Are you not bothered by the fact that a car that pulls in 4pm and leaves at 7am is calculated as having parked for -9 hours?
• 02-22-2009
nynicue
Yes, it bother me a lot. I also do understand why you sarcastic, I think I just need a break, my homework is due next week so I have planty of time to fix it. I see where I am wrong I just need to think how to fix it.

Thank you.
• 02-22-2009
BuzzBuzz
That seems a bit of a complicated way of making the exception for the in/out time. I would have used a while statement to work out whether they were getting the \$11 charge or not, something like this:

Code:

```while (hour_in <= 1600 ? x : y) if (y <= 8) { charge=11; } else blah blah blah```
Just a suggestion of an alternative way of solving that issue.