Rental car mileage overage calculation function

I am trying to create a mileage overage calculation function for a C++ program I am writing. I am having problems getting this function to work properly within the switch statement. What I am trying to achieve (or calculate) is the amount of mileage over the carSize mileage limit * days rented. Each carSize has a different overage/mile calculation rate provided below.

My code so far is:

double calcMilesFee(int miles, int days, char carSize)
{
    double milesFee = 0;
    double compactOverageCharge = .05;
    double midSizeOverageCharge = .07;
    double fullSizeOverageCharge = .09;
    int compactDailyMilesLimit = 20;
    int midSizeDailyMilesLimit = 25;
    int fullSizeDailyMilesLimit = 30;

    switch (carSize)
    {
        case 'c':
        case 'C':
        {
            while (miles > compactDailyMilesLimit * days)
            {
                milesFee =  (miles * days) / compactDailyMilesLimit;
            }
            if (milesFee > compactDailyMilesLimit)
            {
                milesFee = milesFee * compactOverageCharge;
                cout << "Overage Fee is " << milesFee << endl;
                break;
            }
        }


        case 'm':
        case 'M':
        {
            while (miles > midSizeDailyMilesLimit * days)
            {
                milesFee =  (miles * days) / midSizeDailyMilesLimit;
            }
            if (milesFee > midSizeDailyMilesLimit)
            {
                milesFee = milesFee * midSizeOverageCharge;
                cout << "Overage Fee is " << milesFee << endl;
                break;
            }
        }

        case 'f':
        case 'F':
        {
            while (miles > fullSizeDailyMilesLimit * days)
            {
                milesFee =  (miles * days) / fullSizeDailyMilesLimit;
            }
            if (milesFee > fullSizeDailyMilesLimit)
            {
                milesFee = milesFee * fullSizeOverageCharge;
                cout << "Overage Fee is " << milesFee << endl;
                break;
            }
        }
    }
    return milesFee;

}

Answers


Your problems include:

  1. You never break from each switch case label except with the if-conditions.
  2. Your while-loops never end. There is nothing in the body of the while-loops that will ever change the conditional.

That said, this problem is considerably simpler than you're making it out to be:

#include <iostream>
#include <cmath>

double calcMilesFee(int miles, int days, char carSize)
{
    const double compactOverageCharge = .05;
    const double midSizeOverageCharge = .07;
    const double fullSizeOverageCharge = .09;
    const int compactDailyMilesLimit = 20;
    const int midSizeDailyMilesLimit = 25;
    const int fullSizeDailyMilesLimit = 30;
    double overageCharge = 0.0;
    int dailyMilesLimit = 0;
    double milesFee = 0;

    switch (carSize)
    {
        case 'c':
        case 'C':
            dailyMilesLimit = compactDailyMilesLimit;
            overageCharge = compactOverageCharge;
            break;

        case 'm':
        case 'M':
            dailyMilesLimit = midSizeDailyMilesLimit;
            overageCharge = midSizeOverageCharge;
            break;

        case 'f':
        case 'F':
            dailyMilesLimit = fullSizeDailyMilesLimit;
            overageCharge = fullSizeOverageCharge;
            break;
    }

    milesFee = std::max(0, miles - (days * dailyMilesLimit)) * overageCharge;
    std::cout << "Overage Fee is " << milesFee << std::endl;
    return milesFee;
}

As I mentioned in comment, I would advise not using floating-point for currency calculation unless mandatory for your assignment. You can do the final result as a floating point value, but were I writing this i would use integer precision to some hard limit (like 10ths of pennies) and perform the floating point conversion once, and last.


Every case statement should end with break. In your case if any of the inner if statement fails there is no break making next case statement to execute which results different behavior.


Need Your Help

Location updates issue for iOS 7

ios iphone ios7 location

I'm developing an iOS map application, so it's essential to receive location data consistently.

spoon gradle : Could not create plugin of type 'AppPlugin'

java android gradle spoon

my gradle project was build succeed, but when add spoon ,then i got this error (Could not create plugin of type 'AppPlugin')