Thread: calculating garage charges

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Think about what hour_in time(s) are less than 16, and what hour_in times are greater than 16.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also as a hint, what does your calculate_hours return in this case? Is it right?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you, I think I got it , will try to fix

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    No, still did not get it
    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.

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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    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.

  8. #8
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  2. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  3. garage simulation
    By rayrayj52 in forum C++ Programming
    Replies: 14
    Last Post: 11-03-2004, 03:49 PM
  4. Pointer
    By silicon in forum C Programming
    Replies: 2
    Last Post: 03-25-2004, 02:34 PM
  5. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM