Thread: Parking Charges Problem

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Parking Charges Problem

    I am working on the following problem and am stuck...

    5.9
    A parking garage charges a $2.00 minimum fee to park for up to three hours and an additional $.50 per hours for each hour or part thereof over three hours. The maximum charges for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that will calculate and print the parking charges for each of three customers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a neat tabular format, and should calculate and print the total of yesterday's receipts. The program should use the function calculateCharges to determine the charge for each customer.

    Here is my code so far...it compiles fine and the table is formatted correctly, but I am struggling getting the calculations to be right. Does the function need to be in another place? I've tried moving it above the system pause and & return0 but that didn't seem to help at all.

    Any help would be much appreciated!!!! Thanks!

    ........................................
    insert
    Code:
    #include <stdio.h>
    /*
    Goal: Print out garage charges
    Input: Hours Parked
    Output: Car#, hours stayed, and charges
    Requirements: An intenger
    Formulas: charge = 2 + (0.5 * hoursStayed)
    
    Test Plan:
         Car           Hours            Charge
         1             1.5              2.00 
         2             4.0              2.5
         3             24.0             10.00
         Total         29.5             14.50
    */     
    
    
    
    int main(void){
        
        float charge;
        float time1;
        float time2;
        float time3;
        float charges;
        char car1;
        char car2;
        char car3;
        float totalHours;
        float totalCharges;
        float hoursStayed;
        
       
        
        printf("How many hours did each car stay?\n");
        scanf("%f\n%f\n%f\n", &time1, &time2, &time3);
        
        charge = calculateCharges();
        totalHours = time1 + time2 + time3;
        totalCharges = (time1 * charge);
    
    
        printf("\n");
        printf("Car\tHours\t\tCharge\n");
        printf("1\t%f\t%f\n");
        printf("2\t%f\t%f\n");
        printf("3\t%f\t%f\n");
        printf("TOTAL\t%f\t%f\n", &totalHours, &totalCharges);
        
        system("PAUSE");	
        return 0;
    }
    
        
    calculateCharges(){
                           float charge;
                           float hoursStayed;
                           
                           if(hoursStayed < 3){
                           charge = 2.00;
                                      
                 }
                           if(hoursStayed > 3){
                           charge = (2 + (0.5 * (hoursStayed-3)));
                                              
                                             }
    return charge;                                           
                                               }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) The return type of your calculation function is implictly an int since it's not specified. You need to change it to float.

    2) You need to pass the hoursStayed as a parameter to that function, so you can call it for each customer in part from your input in main and return the charge for that customer.

    3) Why are you adding the times up? All these calculations are individual, and the charge of one customer is in no way related to the others.

    4) You don't really need to use three variables for every piece of data pertaining to a customer(time, charge, etc) for the three customers. You can use the same one in a loop:

    Code:
    /*PSEUDOCODE*/
    
    nr_customer := 1;
    
    LOOP UNTIL nr_customer = 3:
    
    READ CUSTOMER TIME
    CALCULATE CUSTOMER CHARGE
    PRINT CUSTOMER CHARGE
    nr_customer++;
    
    ENDLOOP
    Last edited by claudiu; 06-05-2010 at 08:56 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Sorry, but I don't understand...

    Okay, honestly...I have no idea what you just told me I needed to do. I'm super green to this stuff and am not really good with any terms or anything. I modified my code a little and it's at least showing me the hours in the table now, but not the charges. Can you "dumb it down" at all and explain what to do in the simplest terms possible?

    Here's my new code -
    insert
    Code:
    #include <stdio.h>
    /*
    Goal: Print out garage charges
    Input: Hours Parked
    Output: Car#, hours stayed, and charges
    Requirements: An intenger
    Formulas: charge = 2 + (0.5 * hoursStayed)
    
    Test Plan:
         Car           Hours            Charge
         1             1.5              2.00 
         2             4.0              2.5
         3             24.0             10.00
         Total         29.5             14.50
    */     
    
    int main(void){
        
        float charge;
        float charge1;
        float charge2;
        float charge3;
        float time1;
        float time2;
        float time3;
        float charges;
        float totalHours;
        float totalCharges;
        float hoursStayed;
       
    
        printf("How many hours did each car stay?\n");
        scanf("%f\n%f\n%f\n", &time1, &time2, &time3);
        
        charge = calculateCharges();
        totalHours = time1 + time2 + time3;
        totalCharges = (charge1 + charge2 + charge3);
     
        printf("\n");
        printf("Car\tHours\t\tCharge\n");
        printf("1\t%f\t%f\n", time1, charge1);
        printf("2\t%f\t%f\n", time2, charge2);
        printf("3\t%f\t%f\n", time3, charge3);
        printf("TOTAL\t%f\t%f\n", &totalHours, &totalCharges);
        
        system("PAUSE");	
        return 0;
    }
    
        
    calculateCharges(){
                           float charge1;
                           float charge2;
                           float charge3;
                           float hoursStayed;
                           
                           if(hoursStayed < 3){
                           charge1 = 2.00;
                                      
                 }
                           if(hoursStayed > 3){
                           charge1 = (2 + (0.5 * (hoursStayed-3)));
                       
                                          
                                             }
                           if(hoursStayed < 3){
                           charge2 = 2.00;
                                      
                 }
                           if(hoursStayed > 3){
                           charge2 = (2 + (0.5 * (hoursStayed-3)));
                                              
                                             }
                                             
                                                                    if(hoursStayed < 3){
                           charge3 = 2.00;
                                      
                 }
                           if(hoursStayed > 3){
                           charge3 = (2 + (0.5 * (hoursStayed-3)));
                                              
                                             }                                         
                                             
    return charge1, charge2, charge3;                   
                          
                                               }

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    man check the valuds of charge that u assume in test and the values that comes by using the formula.they should be same

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Also, I noticed that you have the function after main(), which is OK, but you don't have a function prototype anywhere stating that you're going to have a function called "calculateCharges". If it compiles and runs, that's cool, but I think it's good form to put a function prototype in the code some where before main(). More info here.

    Claudiu, I see your point in adding the all the times up, but kacebug's got a Test Plan at the very beginning of the "[CODE]" block that shows how he/she wants it printed out. It does match that, though it wasn't mentioned as part of the initial question.
    As far as the
    Code:
    system("PAUSE");
    and
    Code:
    return 0;
    are concerned, that's just going to change the points at which they do that, and I'd say they're fine at where they're at.

    What Claudiu is saying is; you have your function, and in theory, it works. BUT, because it's not explicitly defined anywhere as returning a float, that is
    Code:
     float myfunction(float var1, float var2)
    the compiler is going to take action and assume, and in this case assume incorrectly, that you want it to return an int type number, and because a floating point number is larger then an integer it may give undefined behavior (incorrect results). (google integer overflow).
    He also says that
    2) You need to pass the hoursStayed as a parameter to that function, so you can call it for each customer in part from your input in main and return the charge for that customer.
    What claudiu means is check out how you're using the calculateCharges function and compare it with how the functions look and how they're used in the previous link. Does it look the same when it's used in the tutorial compared to how you're using it? Take a look at claudiu's loop and try to translate it into C code and then see how it works.

    Don't get frustrated, Thomas Edison once said
    I have not failed. I've just found 10,000 ways that won't work.
    Hope it helps.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    man check the valuds of charge that u assume in test and the values that comes by using the formula.they should be same

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why are you being so repetitive? The point of a function is to write how to do something once and then call the function instead of repeating things.
    Code:
    float calculateCharges(float hoursStayed){
       float charge1 = 0.0;                       
    
       if(hoursStayed <= 3.0){
          charge1 = 2.00;                                 
       }
       if(hoursStayed > 3.0){
          charge1 = (2.0 + (0.5 * (hoursStayed-3.0)));
       }
    
       return charge1;
    }
    Something like that.
    Last edited by whiteflags; 06-05-2010 at 10:24 AM.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Your calculateCharges() function can be simplified.

    Code:
    #define MIN_FEE           2.0
    #define MAX_FEE          10.0
    float calculateCharges(float hoursStayed){
       float charge;
       charge = MIN_FEE;              // min amount
       if( hoursStayed >= 24.0 ) {
            return MAX_FEE;
       }
       if(hoursStayed > 3.0)  {              
          charge += 0.5 * (hoursStayed-3.0) ;
       }
    
       return charge;
    }
    Don't forget to put function prototype. If there's no function prototype, compiler assumes function returns int and nothing is assumed about arguments.
    Last edited by Bayint Naung; 06-05-2010 at 10:21 PM.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    Thanks for the help, everyone! I finally got it figured out! I'm so thankful for nice people like you guys and the people in my class who seem to teach me more in a handful of minutes than my instructor has in the entire term! You are all greatness!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

Tags for this Thread