fork download
  1. // Elaine Torrez CS1A Chapter 6, P.374, #17
  2. // *****************************************************************************************
  3. // * PAINT JOB ESTIMATOR *
  4. // *--------------------------------------------------------------------------------------- *
  5. // * This program estimates the cost of a paint job based on the number of rooms, *
  6. // * the square footage of wall space in each room, and the price per gallon of paint. *
  7. // * For every 115 square feet of wall space, 1 gallon of paint and 8 hours of labor are *
  8. // * required. Labor costs $18.00 per hour. *
  9. // *--------------------------------------------------------------------------------------- *
  10. // * INPUT *
  11. // * numRooms : number of rooms to be painted (>= 1) *
  12. // * paintPrice : price per gallon of paint (>= $10.00) *
  13. // * wallSqFt : wall space in each room (>= 0) *
  14. // *--------------------------------------------------------------------------------------- *
  15. // * OUTPUT *
  16. // * gallonsNeeded : total gallons of paint required *
  17. // * laborHours : total hours of labor required *
  18. // * paintCost : cost of the paint *
  19. // * laborCost : total labor charges *
  20. // * totalCost : total cost of the paint job *
  21. // *****************************************************************************************
  22.  
  23. #include <iostream>
  24. #include <iomanip>
  25. #include <limits>
  26. using namespace std;
  27.  
  28. // ---------------- Function Prototypes ----------------
  29. double getPositiveDouble(string prompt, double minValue);
  30. int getPositiveInt(string prompt, int minValue);
  31. double calcGallons(double totalSqFt);
  32. double calcLaborHours(double totalSqFt);
  33. double calcPaintCost(double gallons, double paintPrice);
  34. double calcLaborCost(double hours);
  35. void displayResults(double gallons, double hours, double paintCost, double laborCost, double totalCost);
  36.  
  37. // ---------------------- MAIN -------------------------
  38. int main()
  39. {
  40. const double SQFT_PER_GALLON = 115.0;
  41. const double HOURS_PER_GALLON = 8.0;
  42. const double LABOR_RATE = 18.0;
  43.  
  44. int numRooms;
  45. double paintPrice, wallSqFt, totalSqFt = 0;
  46. double gallons, hours, paintCost, laborCost, totalCost;
  47.  
  48. cout << fixed << setprecision(2);
  49.  
  50. // ---------------------- INPUT ----------------------
  51. numRooms = getPositiveInt("Enter the number of rooms to be painted: ", 1);
  52. paintPrice = getPositiveDouble("Enter the price of paint per gallon: $", 10.0);
  53.  
  54. // Get total square footage from all rooms
  55. for (int i = 1; i <= numRooms; i++)
  56. {
  57. wallSqFt = getPositiveDouble("Enter the square feet of wall space for room #" + to_string(i) + ": ", 0);
  58. totalSqFt += wallSqFt;
  59. }
  60.  
  61. // -------------------- PROCESSING -------------------
  62. gallons = calcGallons(totalSqFt);
  63. hours = calcLaborHours(totalSqFt);
  64. paintCost = calcPaintCost(gallons, paintPrice);
  65. laborCost = calcLaborCost(hours);
  66. totalCost = paintCost + laborCost;
  67.  
  68. // ---------------------- OUTPUT ----------------------
  69. displayResults(gallons, hours, paintCost, laborCost, totalCost);
  70.  
  71. return 0;
  72. }
  73.  
  74. // ---------------- Function Definitions ----------------
  75.  
  76. // Get a validated positive integer
  77. int getPositiveInt(string prompt, int minValue)
  78. {
  79. int value;
  80. cout << prompt;
  81. cin >> value;
  82.  
  83. while (cin.fail() || value < minValue)
  84. {
  85. cin.clear();
  86. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  87. cout << "ERROR: Value must be at least " << minValue << ". Re-enter: ";
  88. cin >> value;
  89. }
  90. return value;
  91. }
  92.  
  93. // Get a validated positive double
  94. double getPositiveDouble(string prompt, double minValue)
  95. {
  96. double value;
  97. cout << prompt;
  98. cin >> value;
  99.  
  100. while (cin.fail() || value < minValue)
  101. {
  102. cin.clear();
  103. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  104. cout << "ERROR: Value must be at least " << minValue << ". Re-enter: ";
  105. cin >> value;
  106. }
  107. return value;
  108. }
  109.  
  110. // Calculate gallons of paint required
  111. double calcGallons(double totalSqFt)
  112. {
  113. const double SQFT_PER_GALLON = 115.0;
  114. return totalSqFt / SQFT_PER_GALLON;
  115. }
  116.  
  117. // Calculate labor hours required
  118. double calcLaborHours(double totalSqFt)
  119. {
  120. const double HOURS_PER_GALLON = 8.0;
  121. const double SQFT_PER_GALLON = 115.0;
  122. return (totalSqFt / SQFT_PER_GALLON) * HOURS_PER_GALLON;
  123. }
  124.  
  125. // Calculate cost of paint
  126. double calcPaintCost(double gallons, double paintPrice)
  127. {
  128. return gallons * paintPrice;
  129. }
  130.  
  131. // Calculate cost of labor
  132. double calcLaborCost(double hours)
  133. {
  134. const double LABOR_RATE = 18.0;
  135. return hours * LABOR_RATE;
  136. }
  137.  
  138. // Display results
  139. void displayResults(double gallons, double hours, double paintCost, double laborCost, double totalCost)
  140. {
  141. cout << "\n----- Paint Job Estimate -----" << endl;
  142. cout << "Gallons of paint required : " << gallons << endl;
  143. cout << "Hours of labor required : " << hours << endl;
  144. cout << "Cost of paint : $" << paintCost << endl;
  145. cout << "Labor charges : $" << laborCost << endl;
  146. cout << "----------------------------------" << endl;
  147. cout << "Total cost of paint job : $" << totalCost << endl;
  148. }
  149.  
Success #stdin #stdout 0.01s 5324KB
stdin
2
25
250
175
stdout
Enter the number of rooms to be painted: Enter the price of paint per gallon: $Enter the square feet of wall space for room #1: Enter the square feet of wall space for room #2: 
----- Paint Job Estimate -----
Gallons of paint required : 3.70
Hours of labor required    : 29.57
Cost of paint              : $92.39
Labor charges              : $532.17
----------------------------------
Total cost of paint job    : $624.57