fork download
  1. // Timothy Price CS1A Ch6 Challenge 17
  2. /*******************************************************************************
  3.  * Calculate estimate
  4.  *
  5.  * This program receives a number of rooms, a cost value of paint, and a square
  6.  * footage value for each room, and calculates an estimate.
  7.  *
  8.  * Inputs:
  9.  * Number of rooms
  10.  * Cost of paint
  11.  * Square feet per room
  12.  *
  13.  * Outputs:
  14.  * Estimate for paint job
  15. *******************************************************************************/
  16.  
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <cmath>
  20.  
  21. using namespace std;
  22.  
  23. // Function prototypes
  24. int get_rooms();
  25. double get_paint();
  26. double get_totalSqFt(int numRooms);
  27. void job_details(double totalSqFt, double paintPrice);
  28.  
  29. // Call all functions to gather info and calculate estimate
  30. int main() {
  31. int rooms = get_rooms();
  32. double paintPrice = get_paint();
  33. double totalSqFt = get_totalSqFt(rooms);
  34.  
  35. job_details(totalSqFt, paintPrice);
  36.  
  37. return 0;
  38. }
  39.  
  40. // Gets and validates the number of rooms
  41. int get_rooms() {
  42. int rooms;
  43. cout << "Enter the number of rooms to be painted: ";
  44. cin >> rooms;
  45. cout << rooms << " rooms." << endl;
  46. while (rooms < 1) {
  47. cout << "Error: Number of rooms must be 1 or greater." << endl;
  48. cout << "Enter the number of rooms to be painted: ";
  49. cin >> rooms;
  50. }
  51. return rooms;
  52. }
  53.  
  54. // Gets and validates the price of paint per gallon
  55. double get_paint() {
  56. double price;
  57. cout << "Enter the price of the paint per gallon ($): ";
  58. cin >> price;
  59. cout << fixed << setprecision(2);
  60. cout << "$" << price << endl;
  61. while (price < 10.00) {
  62. cout << "Error: Paint price cannot be less than $10.00." << endl;
  63. cout << "Enter the price of the paint per gallon ($): ";
  64. cin >> price;
  65. }
  66. return price;
  67. }
  68.  
  69. // Asks for square footage of each room, validates it, and returns the total
  70. double get_totalSqFt(int numRooms) {
  71. double totalSqFt = 0.0;
  72. double roomSqFt;
  73.  
  74. for (int count = 1; count <= numRooms; count++) {
  75. cout << "Enter the square feet of wall space in room " << count << ": ";
  76. cin >> roomSqFt;
  77. cout << roomSqFt << " square feet." << endl;
  78. while (roomSqFt < 0) {
  79. cout << "Error: Square footage cannot be negative." << endl;
  80. cout << "Enter the square feet of wall space in room ";
  81. cout << count << ": ";
  82. cin >> roomSqFt;
  83. }
  84. totalSqFt += roomSqFt;
  85.  
  86. }
  87. cout << totalSqFt << " total square feet." << endl;
  88. return totalSqFt;
  89. }
  90.  
  91. // Calculates and displays the required paint, labor, and costs
  92. void job_details(double totalSqFt, double paintPrice) {
  93. // 1 gal and 8 hrs labor per 115 square feet, as stated by challenge
  94. double gallons = totalSqFt / 115.0;
  95. double laborHours = (totalSqFt / 115.0) * 8.0;
  96. double paintCost = gallons * paintPrice;
  97. double laborCharges = laborHours * 18.00;
  98. double totalCost = paintCost + laborCharges;
  99.  
  100. // Display estimate
  101. cout << fixed << setprecision(2) << endl;
  102. cout << "------- Paint Job Estimate -------" << endl << endl;
  103. cout << "Gallons of paint required: " << gallons << endl;
  104. cout << "Hours of labor required: " << laborHours << endl;
  105. cout << "Cost of the paint: $" << paintCost << endl;
  106. cout << "Labor charges: $" << laborCharges << endl;
  107. cout << "---------------------------------------" << endl;
  108. cout << "Total cost of the job: $" << totalCost << endl;
  109. }
  110.  
Success #stdin #stdout 0.01s 5288KB
stdin
6
23.50
250
300
300
100
450
700
stdout
Enter the number of rooms to be painted: 6 rooms.
Enter the price of the paint per gallon ($): $23.50
Enter the square feet of wall space in room 1: 250.00 square feet.
Enter the square feet of wall space in room 2: 300.00 square feet.
Enter the square feet of wall space in room 3: 300.00 square feet.
Enter the square feet of wall space in room 4: 100.00 square feet.
Enter the square feet of wall space in room 5: 450.00 square feet.
Enter the square feet of wall space in room 6: 700.00 square feet.
2100.00 total square feet.

------- Paint Job Estimate -------

Gallons of paint required: 18.26
Hours of labor required:   146.09
Cost of the paint:         $429.13
Labor charges:             $2629.57
---------------------------------------
Total cost of the job:     $3058.70