This is as far as I can make it. Can anyone tell me exactly where I'm going wrong.

This is supposed to be a car rental program that states what the vehicle rental cost is and the vehicles that are cheaper. I'm. Not getting g the right numbers when it runs.



#include <iostream>
using namespace std;


double addVehicleCost(double overageRate, int freeMiles, double dailyFee, int estimatedMiles);
int main()
{

const double CLASS_E_DAILY = 45.90;
const int CLASS_E_FREE_MILES = 40;
const double CLASS_E_OVERAGE_RATE = 0.25;
const double CLASS_S_DAILY = 57.75;
const int CLASS_S_FREE_MILES = 120;
const double CLASS_S_OVERAGE_RATE = 0.40;
const double CLASS_L_DAILY = 85.50;
const int CLASS_L_FREE_MILES = 150;
const double CLASS_L_OVERAGE_RATE = 0.50;
const double CLASS_V_DAILY = 55.00;
const int CLASS_V_FREE_MILES = 150;
const double CLASS_V_OVERAGE_RATE = 0.50;
const double CLASS_T_DAILY = 55.00;
const int CLASS_T_FREE_MILES = 150;
const double CLASS_T_OVERAGE_RATE = 0.50;

char classOfVehicle;
int estimatedMiles;
double selectedVehicleCost, eCost, sCost, lCost, vCost, tCost;

cout << "Enter the class of the vehicle: ";
cin >> classOfVehicle;
cout << "Please enter the estimated miles (1 to 812): ";
cin >> estimatedMiles;

eCost = addVehicleCost(CLASS_E_DAILY, CLASS_E_FREE_MILES, CLASS_E_OVERAGE_RATE, estimatedMiles);
lCost = addVehicleCost(CLASS_L_DAILY, CLASS_L_FREE_MILES, CLASS_L_OVERAGE_RATE, estimatedMiles);
sCost = addVehicleCost(CLASS_S_DAILY, CLASS_S_FREE_MILES, CLASS_S_OVERAGE_RATE, estimatedMiles);
vCost = addVehicleCost(CLASS_V_DAILY, CLASS_V_FREE_MILES, CLASS_V_OVERAGE_RATE, estimatedMiles);
tCost = addVehicleCost(CLASS_T_DAILY, CLASS_T_FREE_MILES, CLASS_T_OVERAGE_RATE, estimatedMiles);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

switch (classOfVehicle)
{
case 'E':
case 'e':
cout << "Economy vehicle cost: $" << eCost << endl;
selectedVehicleCost = eCost;
break;
case 'S':
case 's':
cout << "Sport vehicle cost: $" << sCost << endl;
selectedVehicleCost = sCost;
break;
case 'L':
case 'l':
cout << "Luxury vehicle cost: $" << lCost << endl;
selectedVehicleCost = lCost;
break;
case 'V':
case 'v':
cout << "Van vehicle cost: $" << vCost << endl;
selectedVehicleCost = vCost;
break;
case 'T':
case 't':
cout << "Truck vehicle cost: $" << tCost << endl;
selectedVehicleCost = tCost;
break;
}

if (eCost < selectedVehicleCost)
{
cout << "Economy is cheaper by: $" << selectedVehicleCost - eCost << endl;
}
if (sCost < selectedVehicleCost)
{
cout << "Sport is cheaper by: $" << selectedVehicleCost - sCost << endl;
}
if (lCost < selectedVehicleCost)
{
cout << "Luxury is cheaper by: $" << selectedVehicleCost - lCost << endl;
}
if (vCost < selectedVehicleCost)
{
cout << "Van is cheaper by: $" << selectedVehicleCost - vCost << endl;
}
if (tCost < selectedVehicleCost)
{
cout << "Truck is cheaper by: $" << selectedVehicleCost - tCost << endl;
}

return 0;
}
double addVehicleCost(double overageRate, int freeMiles, double dailyFee, int estimatedMiles)

{
double vehicleCost = dailyFee;

if (estimatedMiles > freeMiles)
{
vehicleCost = (estimatedMiles - freeMiles) * overageRate;
}
return vehicleCost;
}
Last edited on
I'm. Not getting g the right numbers when it runs.


What numbers would you like to get from what input?


You call functions like:
eCost = addVehicleCost(CLASS_E_DAILY, CLASS_E_FREE_MILES, CLASS_E_OVERAGE_RATE, estimatedMiles);
but you defined your function as
double addVehicleCost(double overageRate, int freeMiles, double dailyFee, int estimatedMiles)

It looks to me like the order of your first and third arguments is inconsistent.




When you prompt the user to enter something:
1
2
cout << "Enter the class of the vehicle: ";
cin >> classOfVehicle;

you should tell him what his options are ... or he won't have a clue.


Please put your code in code tags.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <iomanip>

enum vehicle_class                  { ECONOMY=0, SPORT=1, LUXURY=2, VAN=3, TRUCK=4 };
const int N = TRUCK+1 ; // number of vehicle types
const std::string vehicle_name[N] = { "Economy", "Sport", "Luxury", "Van", "Truck" } ;
const double daily_cost[N] =        { 45.90,      57.75,   85.50,   55.00,  55.00  } ;
const int free_miles[N] =           { 40,         120,     150,     150,    150    } ;
const double overage_rate[N] =      { 0.25,       0.40,    0.50,    0.50,   0.50   } ;

vehicle_class get_vehicle_class()
{
    std::cout << "vehicle classes:\n-------------\n" ;

    std::string valid_chars ;
    for( const std::string& name : vehicle_name )
    {
        std::cout << name.front() << ". " << name << '\n' ;
        valid_chars += name.front() ;
    }

    std::cout << "\nEnter the class of the vehicle [one of " << valid_chars << "]: " ;
    char vc ;
    std::cin >> vc ;

    vc = std::toupper( (unsigned char)vc ) ;
    for( int i = 0 ; i<N ; ++i )
        if( vehicle_name[i].front() == vc ) return vehicle_class(i) ;

    std::cout << "invalid vehicle class. try again\n\n" ;
    return get_vehicle_class() ;
}

double get_miles()
{
    const double min_miles = 1 ;
    const double max_miles = 812 ;

    std::cout << "Please enter the estimated miles ("
              << min_miles << " to " << max_miles << "): ";
    double miles ;
    if( std::cin >> miles )
    {
        if( miles >= min_miles && miles <= max_miles ) return miles ; // valid input, return it

        else std::cout << "invalid value for estimated miles. try again\n" ;
    }
    else
    {
        std::cout << "error: non-numeric input. try again\n" ;
        std::cin.clear() ; // clear the failed stated
        std::cin.ignore( 1000, '\n' ) ; // throw the bad input line away
    }

    return get_miles() ; // try again
}

double cost_for_the_day( vehicle_class selected_vehicle, double miles )
{
    double cost = daily_cost[selected_vehicle] ;
    const double free_dist = free_miles[selected_vehicle] ;
    if( miles > free_dist ) cost += (miles-free_dist) * overage_rate[selected_vehicle] ;
    return cost ;
}

int main()
{
    const vehicle_class vc = get_vehicle_class() ;
    const double miles = get_miles() ;

    const double cost = cost_for_the_day( vc, miles ) ;

    std::cout << "vehicle '" << vehicle_name[vc] << "' costs GBP "
              << std::fixed << std::setprecision(2) << cost
              << " for " << miles << " miles.\n" ;
}
Thank you so much! I'm a beginner and we are just learning functions. I appreciate the help.
Topic archived. No new replies allowed.