A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge 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 3 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.
#include <iostream>
#include <iomanip>
using namespace std;
double calculateChargees(double hours) {
double charges = 2.0;
int iHours = static_cast<int>(hours +0.99);
charges += 0.5 * (iHours - 3);
if (charges > 10.0) {
charges = 10.0;
}
return charges;
}
int main() {
double hours[3];
double charges[3], total=0.0;
for (int i=0; i<3; i++) {
cout << "Enter parking time for the customer " << i+1 << ": ";
cin >> hours[i];
charges[i] = calculateChargees(hours[i]);
total += charges[i];
}
cout << endl;
cout << "Cust # | Hours | Charge" << endl;
cout << "-------+-------+-------" << endl;
for (int i=0; i<3; i++) {
cout << setw(6) << i+1 << " | ";
cout << fixed << setprecision(2) << setw(5) << hours[i] << " | ";
cout << "$" << setw(5) << charges[i] << endl;
}
cout << "-------+-------+-------" << endl;
cout << " Total charges $" << setw(5) << total << endl;
return 0;
}
Comments
Leave a comment