Last active
December 19, 2021 11:43
-
-
Save Chamauta/70aa532e34886ca206bf83c148861a21 to your computer and use it in GitHub Desktop.
Deitel_Chapter_06_Exercise_12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*6.12 (Parking Charges) 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 calculates and prints 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 formatand should calculate | |
and print the total of yesterday’s receipts.The program should use the function calculateCharges to | |
determine the charge for each customer.Your outputs should appear in the following format : | |
Car Hours Charge | |
1 1.5 2.00 | |
2 4.0 2.50 | |
3 24.0 10.00 | |
TOTAL 29.5 14.50*/ | |
// =============================================== 06-12 =============================================== // | |
#include <iostream> | |
#include <iomanip> | |
void displayPrompt() // display first output and prompt user to enter limit variable and hour data | |
{ | |
std::cout << "Assume all cars park for no longer than 24 hours." << std::endl; | |
std::cout << "Enter the number of cars parked: "; // 1st prompt | |
} | |
int getCarsParked() | |
{ | |
int limit; // the number of parking lot customers for this program | |
displayPrompt(); | |
std::cin >> limit; | |
return limit; | |
} | |
void displayTableHeader() | |
{ | |
std::cout << "\n" | |
<< std::left << std::setw(5) << "Car" | |
<< std::right << std::setw(10) << "Hours" | |
<< std::right << std::setw(15) << "Charge" | |
<< std::endl; | |
} | |
double calculateCharges(double &hoursRef) | |
{ | |
double charges; | |
if (hoursRef <= 3.0) | |
charges = 2.0; | |
else if (hoursRef > 3.0 && hoursRef <= 24.00) | |
charges = 2.0 + ceil((hoursRef - 3.0)) * 0.50; // use ceil for "or part thereof"; see problem statement | |
else | |
charges = 10.00; // in case user enters a value > 24 hours | |
if (charges >= 10.0) // failsafe | |
charges = 10.0; | |
return charges; | |
} | |
void displayTable(int &countRef, double &hoursRef, double &chargesRef) | |
{ | |
std::cout << std::fixed | |
<< std::left << std::setw(5) << countRef | |
<< std::setprecision(1) << std::right << std::setw(10) << hoursRef | |
<< std::setprecision(2) << std::right << std::setw(15) << chargesRef << std::endl; | |
} | |
void displayTotal( double &totalHoursRef, double &totalChargeRef ) | |
{ | |
std::cout | |
<< std::left << std::setw(5) << "TOTAL" | |
<< std::setprecision(1) << std::right << std::setw(10) << totalHoursRef | |
<< std::setprecision(2) << std::right << std::setw(15) << totalChargeRef | |
<< std::endl << std::endl; | |
} | |
int main() | |
{ | |
double hours{ 0 }; // the number of hours entered, initialized to 0 | |
int count{ 1 }; // tallies the number entries, initialized to 1 | |
double charges; // calculate variable charge | |
double totalCharge{ 0 }; // running total of charges | |
double totalHours{ 0 }; // ditto for hours | |
int limit = getCarsParked(); | |
std::cout << "Enter hours parked for " << limit << " customers separated each by a space: "; // 2nd prompt | |
while (true) | |
{ | |
std::cin >> hours; // number of hours parked are streamed into variable hours | |
if (count == 1) // print table header | |
displayTableHeader(); | |
charges = calculateCharges(hours); // calculate charges | |
displayTable(count, hours, charges); // diplay results in a table | |
++count; // increments count after each cin stream pass; | |
totalHours += hours; // adds hours to totalhours running total | |
totalCharge += charges; // adds charges to totalcharges running total | |
if (count > limit)// print output of totals when done | |
{ | |
displayTotal(totalHours, totalCharge); | |
break; // end loop and exit program | |
} | |
} | |
}// end of parkingCharges problem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment