# Program to calculate charge for parked cars

• 03-17-2005
strider496
Program to calculate charge for parked cars
This is the program

A parking garage charges \$2 for minimum fee to park up to 3 hours and then a \$.50 per hour charge after the 3rd hour. The maximum charge for any 24 hour parking is \$10. Assume no car parks longer than 24 hrs, write a program that calculates and prints the parking charge for each of 3 customer. Result should be printed in tabular format. Hint: use function called calculateCHarge to determine charge for each customer

Code:

``` #include <stdio.h> minfee = 2; int main() {         int car1,car2,car3,hour,hour2,hour3,charge1,charge2,charge3,                 totalhour,totalcharge,minfee;         printf("Enter car number\n");         scanf("%d", &car1);         printf("How many hour did the car park\n");         scanf("%d", &hour);         if (hour>=3) {                 charge1 = (minfee+hour*.50); }         else {                 charge1 = 2.00; }         printf("Enter car number\n");         scanf("%d", &car2);         printf("How many hour did the car park\n");         scanf("%d", &hour2);         if (hour>=3) {                 charge2 = (minfee+hour*.50); }         else {                 charge2 = 2.00; }         printf("Enter car number\n");         scanf("%d", &car3);         printf("How many hour did the car park\n");         scanf("%d", &hour3);         if (hour>=3) {                 charge3 = (minfee+hour*.50); }         else {                 charge3 = 2.00; }         totalhour=(hour+hour2=hour3);         printf("%d", totalhour);         totalcharge=(charge1+charge2+charge3);         printf("%d", totalcharge); return 0; }```
Heres how output should be

Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
Total 29.5 14.50
• 03-18-2005
Krak
Code:

```int car1,car2,car3,hour,hour2,hour3,charge1,charge2; if (hour>=3) {                 charge2 = (minfee+hour*.50); //Very likely loss of data }```
Also dear, rather than having 3 variables for cars, charges, etc. and going through that process 3 times, a simple loop of some kind or just a function could easily serve your purposes, with greater efficiency and less code. You see hun, they have these things call arrays for this. An array is a collection of data of the same type under one name.

Observe:
Code:

```int car1, car2, car3, car4, car5;    //Stupid way to do things. int cars[5];                          //Fun way to do things.```
I'm not sure if you know about arrays and loops or not, but if not, I suggest you read up on them because they're unbelievably useful, especially used in conjunction with each other.

Also....
Really, how hard is it to proof-read?? Honestly.
Quote:

printf("How many hour[s] did the car park[?]\n");
And "minfee" has no type declared.
• 03-19-2005
7smurfs
Quote:

use function called calculateCHarge to determine charge for each customer
Doing so will clean up your main(), which (in my opinion) is always nice, make it easier to code and calculate, and above all: make it easier to understand where a problem lies if you find out you have a bug in it.
• 03-19-2005
Karthur
Also, you tried to use minfee before you assigned it a value (never did), and didn't use it when you needed the minimum only (hard coded 2.00 instead), and finally you forgot that 2.00 + (21 * 0.50) is greater than maxfee which is 10.00. So I suggest in your loop you
subtract 3 hours,
if hours not negative, add (hours * 0.50)
subtract 16 hours
if hours not negative, subtract (hours * 0.50)
return result.
• 03-20-2005
strider496
revised program
Code:

``` #include <stdio.h> int main()         {                 int car,counter=0;                 float minfee=2.00,maxfee=10.00,hours,totcharge,tothours,                         calculateCharge;                 totcharge=0;                 tothours=0;                 counter=1;                 while(counter<=3){                                 printf("enter car number\n");                 scanf("%d",&car);                 printf("enter hours parked\n");                 scanf("%f",&hours);                                                 if(hours<4) {                                 calculateCharge=minfee;                                 printf("%.2f",calculateCharge);                         }                         if(hours<=24) {                                 calculateCharge=(minfee+hours/2);                                 printf("%.2f",calculateCharge);                         }                         else  {                                 calculateCharge=maxfee;                                 printf("%.2f",calculateCharge);                         }                 counter=counter+1;                 }                 while(hours>=3) {                         calculateCharge=(minfee+hours/2);                                 tothours=tothours+hours;                 printf("%.2f",tothours);                 totcharge=totcharge+calculateCharge;                 printf("%.2f",totcharge);       return 0; }```
I dont know why this is not printing tothours and totalcharge.Does any1 know?
• 03-20-2005
Dave_Sinkula
Fix the syntax error for the missing } so that we know what you are acutally attempting to run.
• 03-20-2005
strider496

enter car number
1
enter hours parked
1.5
enter car number
2
enter hours parked
4.0
enter car number
3
enter hours parked
24.0
then shows infinite numbers scrolling down

result should be

Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
Total 29.5 14.50