Thread: Program to calculate charge for parked cars

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    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

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    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.
    printf("How many hour[s] did the car park[?]\n");
    And "minfee" has no type declared.
    Last edited by Krak; 03-18-2005 at 01:26 AM.

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Also, I suggest you take up your assignment's advice:
    use function called calculateCHarge to determine charge for each customer
    and write a function to help you.
    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.
    To code is divine

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    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
    start with 2.00,
    subtract 3 hours,
    if hours not negative, add (hours * 0.50)
    subtract 16 hours
    if hours not negative, subtract (hours * 0.50)
    return result.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    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?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Fix the syntax error for the missing } so that we know what you are acutally attempting to run.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    my result was this

    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
    Last edited by strider496; 03-21-2005 at 12:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Program to calculate factorials
    By forzamilan in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2001, 10:24 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM