Can someone help with an equation?

I have to make a program that calculates the bill for renting a car. The problem is I have to make it calculate the days rental fee for the car like this.

Cars rented for 7 or more days are charged a lower weekly rate for whole weeks. The discount is applied to the daily rate.

Car Charge Weekly Rate (for 7 days): 6.5 x Daily Rate.
Examples are:

Rate Examples: 12 days = 1 x weekly rate + 5 x daily rate
14 days = 2 x weekly rate
15 days = 2 x weekly rate + 1 x daily rate

This is what I have so far. This calculation must be done in the function double calculateDayCharge(int days, char carSelected. If someone could help me with this I would really appreciate it! I'm having a hard time with it.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
const string COMPACT = "     C - Compact size at $22.91 per day";
const string MID_SIZE = "     M - Mid size at $25.76 per day";
const string FULL_SIZE = "     F - Full size at $28.87 per day";
const string SUV = "     S - SUV at $98.88 per day";
const string NUM_OF_DAYS = "Enter number of days rented: ";
const string MILES_DRIVEN = "Enter number of miles driven: ";
const double C_CHARGE = 22.91;
const double M_CHARGE = 25.76;
const double F_CHARGE = 28.87;
const double S_CHARGE = 98.88;
const double WEEK_RATE = 6.5;

double calculateDayCharge(int days, char carSelected);
int numberPrompt(string prompt);
char displayMenu();
int main()
{
	char usersCarSelection;
	int daysRented;
	int milesDriven;
	double carCharge;
	
	cout << "This program will calculate a car rental bill for Rent2U" << endl << endl;
	
	usersCarSelection = displayMenu();
	daysRented = numberPrompt(NUM_OF_DAYS);
	milesDriven = numberPrompt(MILES_DRIVEN);
	

	
 cout << endl << endl;
 system ("PAUSE");
 
 return 0;
}

char displayMenu()
{
	char carSelect;
	
	cout << "Car sizes:" << endl;
	cout << COMPACT << endl << MID_SIZE << endl << FULL_SIZE << endl << SUV << endl << endl;
	cout << "Enter letter for car size rented: ";
	cin >> carSelect;
	cout << endl << endl;
	
	carSelect = toupper(carSelect);
	
	while((carSelect != 'C') && (carSelect != 'M') && (carSelect != 'F') && (carSelect != 'S'))
	{
		cout << "Invalid Entry: Must enter either C, M, F, or S. Try again: ";
		cin >> carSelect;
		cout << endl << endl;
	}
	
	return carSelect;
}

int numberPrompt(string prompt)
{
	int value;
	cout << prompt;
	cin >> value;
	cout << endl << endl;
	
	while(value < 1)
	{
		cout << "Invalid Entry: Must be a whole number greater than one: ";
		cin >> value;
		cout << endl << endl;
	}
	return value;
}

double calculateDayCharge(int days, char carSelected)
{
	if(daysRented <= 6)
	{
		if(usersCarSelection == 'C')
		{
			carCharge = C_CHARGE * daysRented;
		}
		if(usersCarSelection == 'M')
		{
			carCharge = M_CHARGE * daysRented;
		}		
		if(usersCarSelection == 'F')
		{
			carCharge = F_CHARGE * daysRented;
		}
		if(usersCarSelection == 'S')
		{
			carCharge = S_CHARGE * daysRented;
		}						
	}
	
	else if (daysRented > 6)
	{
		if(usersCarSelection == 'C')
		{
			carCharge = M_CHARGE * daysRented;
		}
		if(usersCarSelection == 'M')
		{
			carCharge = M_CHARGE * daysRented;
		}		
		if(usersCarSelection == 'F')
		{
			carCharge = F_CHARGE * daysRented;
		}
		if(usersCarSelection == 'S')
		{
			carCharge = S_CHARGE * daysRented;
		}
	}
}
Last edited on
You are not using the correct variable names in double calculateDayCharge; days is different to daysRented, ect. You have also not declared carCharge in this function. You are also not returning a value from said function.

Last edited on
Thanks man, I'm new to this and this program is all user defined functions which is confusing to a newbie. Here is what I have so far, its not working correctly yet but it's getting there.

I think whats confusing me about this is the calculateTotalBill function, it calls two other functions, and is also supposed to have four reference parameters, which I am brand new to.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
const string COMPACT = "     C - Compact size at $22.91 per day";
const string MID_SIZE = "     M - Mid size at $25.76 per day";
const string FULL_SIZE = "     F - Full size at $28.87 per day";
const string SUV = "     S - SUV at $98.88 per day";
const string NUM_OF_DAYS = "Enter number of days rented: ";
const string MILES_DRIVEN = "Enter number of miles driven: ";
const double C_CHARGE = 22.91;
const double M_CHARGE = 25.76;
const double F_CHARGE = 28.87;
const double S_CHARGE = 98.88;
const double WEEK_RATE = 6.5;
const double C_MILES = .05;
const double M_MILES = .07;
const double F_MILES = .09;

void displayResults(double chargeForCar, int rentDays, int driveMiles, 
double mileageChargeT, double taxed, double totalBillT, string sizeCar);
void calculateTotalBill(double& carCharge, double& milesDrivenCharge, double& tax, double& grandTotal,
char selectedCar, int daysDriven, int miles);
double calculateMileCharge(int milesDriven, char typeOfCar);
double calculateDayCharge(int days, char carSelected);
int numberPrompt(string prompt);
char displayMenu();
int main()
{
	char usersCarSelection;
	int daysRented;
	int milesDriven;
	double carCharge;
	double milesDrivenCharge;
	double tax;
	double grandTotal;
	string sizeCar;
	char userEnters;
	
	tax = .115;
	
	cout << "This program will calculate a car rental bill for Rent2U" << endl << endl;
	
	do
	{
		usersCarSelection = displayMenu();
		daysRented = numberPrompt(NUM_OF_DAYS);
		milesDriven = numberPrompt(MILES_DRIVEN);
		carCharge = calculateDayCharge(daysRented, usersCarSelection);
		milesDrivenCharge = calculateMileCharge(milesDriven, usersCarSelection);
		calculateTotalBill(carCharge, milesDrivenCharge, tax, grandTotal,
		usersCarSelection, daysRented, milesDriven);
	
		if(usersCarSelection == 'C')
		{
			sizeCar = "Compact";
		}
		else if(usersCarSelection == 'M')
		{
			sizeCar = "Mid-Size";
		}
		else if(usersCarSelection == 'F')
		{
			sizeCar = "Full-Size";
		}
		else if(usersCarSelection == 'S')
		{
			sizeCar = "SUV";
		}	

		
		
		displayResults(carCharge, daysRented, milesDriven, 
		milesDrivenCharge, tax, grandTotal, sizeCar);
		
		cout << "Calculate another bill (y/n)? ";
		cin >> userEnters;
		cout << endl;
		
		userEnters == toupper(userEnters);
	}
	while(userEnters == 'Y');
	
	 cout << endl << endl;
 system ("PAUSE");
 
 return 0;
}

char displayMenu()
{
	char carSelect;
	
	cout << "Car sizes:" << endl;
	cout << COMPACT << endl << MID_SIZE << endl << FULL_SIZE << endl << SUV << endl << endl;
	cout << "Enter letter for car size rented: ";
	cin >> carSelect;
	cout << endl << endl;
	
	carSelect = toupper(carSelect);
	
	while((carSelect != 'C') && (carSelect != 'M') && (carSelect != 'F') && (carSelect != 'S'))
	{
		cout << "Invalid Entry: Must enter either C, M, F, or S. Try again: ";
		cin >> carSelect;
		cout << endl << endl;
	}
	
	return carSelect;
}

int numberPrompt(string prompt)
{
	int value;
	cout << prompt;
	cin >> value;
	cout << endl << endl;
	
	while(value < 1)
	{
		cout << "Invalid Entry: Must be a whole number greater than one: ";
		cin >> value;
		cout << endl << endl;
	}
	return value;
}

double calculateDayCharge(int days, char carSelected)
{
	double carCharge;
	
	if(days <= 6)
	{
		if(carSelected == 'C')
		{
			carCharge = C_CHARGE * days;
		}
		else if(carSelected == 'M')
		{
			carCharge = M_CHARGE * days;
		}		
		else if(carSelected == 'F')
		{
			carCharge = F_CHARGE * days;
		}
		else if(carSelected == 'S')
		{
			carCharge = S_CHARGE * days;
		}						
	}
	
	else if (days > 6)
	{	
		if(carSelected == 'C')
		{
			while(days >= 7)
			{
				carCharge = C_CHARGE * WEEK_RATE;
				days = days - 7;			
			}
			carCharge = C_CHARGE * days;
		}
		
		else if(carSelected == 'M')
		{
			while(days >= 7)
			{
				carCharge = M_CHARGE * WEEK_RATE;
				days = days - 7;			
			}
			carCharge = M_CHARGE * days;
		}		
		
		else if(carSelected == 'F')
		{
			while(days >= 7)
			{
				carCharge = F_CHARGE * WEEK_RATE;
				days = days - 7;			
			}
			carCharge = F_CHARGE * days;
		}
		
		else if(carSelected == 'S')
		{
			while(days >= 7)
			{
				carCharge = S_CHARGE * WEEK_RATE;
				days = days - 7;			
			}
			carCharge = S_CHARGE * days;
		
		}
	}
	return carCharge;
}

double calculateMileCharge(int milesDriven, char typeOfCar)
{
	double milesCharge;
	
	if(typeOfCar == 'C')
	{
		while(milesDriven >= 20)
		{
			milesCharge = (milesDriven - 20) * C_MILES;
		}
	}
	else if(typeOfCar == 'M')
	{
		while(milesDriven >= 20)
		{
			milesCharge = (milesDriven - 20) * M_MILES;
		}
	}
	else if(typeOfCar == 'F')
	{
		while(milesDriven >= 20)
		{
			milesCharge = (milesDriven - 20) * F_MILES;
		}
	}
	
	return milesCharge;
}

void calculateTotalBill(double& carCharge, double& milesDrivenCharge, double& tax, double& grandTotal,
char selectedCar, int daysDriven, int miles)
{
	tax = .115;
	carCharge = calculateDayCharge(daysDriven, selectedCar);
	milesDrivenCharge = calculateMileCharge(miles, selectedCar);
	grandTotal = (carCharge + milesDrivenCharge) * tax;
}

void displayResults(double chargeForCar, int rentDays, int driveMiles, 
double mileageChargeT, double taxed, double totalBillT, string carSize)
{
	cout << "RENT2U RENTAL DETAILS" << endl;
	cout << "     Car Size:" << setw(20) << carSize << endl;
	cout << "     Days Rented:" << setw(20) << rentDays << endl;
	cout << "     Miles Driven:" << setw(20) << driveMiles << endl << endl;
	cout << "BILL" << endl;
	cout << "     Car Charge:" << setw(20) << "$ " << chargeForCar << endl;
	cout << "     Mileage Charge:" << setw(20) << "$ " << mileageChargeT << endl;
	cout << "     Tax:" << setw(20) << "$ " << taxed << endl;
	cout << "-------------------------------------------------------" << endl;
	cout << "     Total Bill:" << setw(20) << "$ " << totalBillT << endl;
}
Last edited on
calculateDayCharge() can be made a lot simpler by computing the daily rate for the car selected, then using that later on. Also, you can compute the value easier by using some arithmetic to get the total number of weeks and then the total number of left over days:
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
double
calculateDayCharge(int days, char carSelected)
{
    double carCharge;
    double dailyRate;

    // calculate the daily rate for the selected car
    if (carSelected == 'C') {
	dailyRate = C_CHARGE;
    } else if (carSelected == 'M') {
	dailyRate = M_CHARGE;
    } else if (carSelected == 'F') {
	dailyRate = F_CHARGE;
    } else if (carSelected == 'S') {
	dailyRate = S_CHARGE;
    }
    
    // Compute the number of weeks rented and the weekly charge.  note
    // that this produces zero if the car was rented for less than one
    // week
    int weeks = days/7;
    carCharge = weeks * dailyRate * 6.5;

    // Chage days to the number of days left over after the weeks
    // that you rented.  "%" is the modulo (remainder) operator
    days = days % 7;
    carCharge = carCharge + days * dailyRate;
    return carCharge;
}


By the way, this whole program could be much shorter if you use arrays and structures. Have you learned about those yet?


Thank you! I have learned control structures, but no data structures or arrays yet. I'm 6 weeks in to my first programming course.
I'm 6 weeks in to my first programming course.

You're off to a good start. :)
Topic archived. No new replies allowed.