fork download
  1. //Cameron Pham CS1A Chapter 6, p. 374, #17
  2. //
  3. /******************************************************************************
  4. * ESTIMATE COST OF PAINT JOB
  5. * ______________________________________________________________________________
  6. * This program accepts floating values for number of rooms and cost of paint per
  7. * gallon. It will then calculate the total cost of the paint job.
  8. * ______________________________________________________________________________
  9. * INPUT
  10. * Rooms // Number of rooms that need to be painted
  11. * Paint cost // Cost of paint per gallon
  12. *
  13. * OUTPUT
  14. * Total cost // Total cost of paint job
  15. *
  16. *******************************************************************************/
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <cmath>
  20. using namespace std;
  21.  
  22. // Constant values
  23. const double sqftPerGallon = 115.0;
  24. const double hrsPerGallon = 8.0;
  25. const double hourlyLaborRate = 18.00;
  26.  
  27. // Function prototypes
  28. int getNumRooms();
  29. double getPaintPrice();
  30. double getWallSpace(int room);
  31. double calculateGallons(double totalSquareFeet);
  32. double calculateLaborHours(double gallons);
  33. double calculatePaintCost(double gallons, double pricePerGallon);
  34. double calculateLaborCost(double laborHours);
  35.  
  36. int main()
  37. {
  38. // Initialize variables
  39. int rooms; // Number of rooms to paint
  40. double paintPrice; // Price per gallon of paint
  41. double wallSpace; // Wall space in each room
  42. double totalSquareFeet = 0; // Total wall space
  43. double gallons; // Gallons of paint required
  44. double laborHours; // Hours of labor required
  45. double paintCost; // Total cost of paint
  46. double laborCost; // Total labor charges
  47. double totalCost; // Total cost of the paint job
  48.  
  49. // Get the number of rooms and paint price
  50. rooms = getNumRooms();
  51. paintPrice = getPaintPrice();
  52.  
  53. // Get the wall space for each room
  54. for (int room = 1; room <= rooms; room++)
  55. {
  56. wallSpace = getWallSpace(room);
  57. totalSquareFeet += wallSpace;
  58. }
  59.  
  60. // Calculate the paint and labor requirements
  61. gallons = calculateGallons(totalSquareFeet);
  62. laborHours = calculateLaborHours(gallons);
  63.  
  64. // Calculate the costs
  65. paintCost = calculatePaintCost(gallons, paintPrice);
  66. laborCost = calculateLaborCost(laborHours);
  67. totalCost = paintCost + laborCost;
  68.  
  69. // Display the results
  70. cout << fixed << setprecision(2);
  71. cout << endl;
  72. cout << "Gallons of paint required: " << gallons << endl;
  73. cout << "Hours of labor required: " << laborHours << endl;
  74. cout << "Cost of paint: $" << paintCost << endl;
  75. cout << "Labor charges: $" << laborCost << endl;
  76. cout << "Total cost of paint job: $" << totalCost << endl;
  77.  
  78. return 0;
  79. }
  80.  
  81. // Ask for the number of rooms and validate the input
  82. int getNumRooms()
  83. {
  84. int rooms;
  85.  
  86. cout << "Enter the number of rooms to be painted: ";
  87. cin >> rooms;
  88. cout << endl;
  89.  
  90. while (rooms < 1)
  91. {
  92. cout << "Please enter at least 1 room: ";
  93. cin >> rooms;
  94. }
  95.  
  96. return rooms;
  97. }
  98.  
  99. // Ask for the price of paint per gallon and validate the input
  100. double getPaintPrice()
  101. {
  102. double price;
  103.  
  104. cout << "Enter the price of paint per gallon (at least $10.00): $";
  105. cin >> price;
  106. cout << endl;
  107.  
  108. while (price < 10.00)
  109. {
  110. cout << "Price must be at least $10.00. Enter again: $";
  111. cin >> price;
  112. }
  113.  
  114. return price;
  115. }
  116.  
  117. // Ask for the wall space in a room and validate the input
  118. double getWallSpace(int room)
  119. {
  120. double squareFeet;
  121.  
  122. cout << "Enter the square feet of wall space in room "
  123. << room << ": ";
  124. cin >> squareFeet;
  125. cout << endl;
  126.  
  127. while (squareFeet < 0)
  128. {
  129. cout << "Square footage cannot be negative. Enter again: ";
  130. cin >> squareFeet;
  131. }
  132.  
  133. return squareFeet;
  134. }
  135.  
  136. // Calculate gallons of paint needed, rounding up to a whole gallon
  137. double calculateGallons(double totalSquareFeet)
  138. {
  139. return ceil(totalSquareFeet / sqftPerGallon);
  140. }
  141.  
  142. // Calculate the labor hours required
  143. double calculateLaborHours(double gallons)
  144. {
  145. return gallons * hrsPerGallon;
  146. }
  147.  
  148. // Calculate the total cost of paint
  149. double calculatePaintCost(double gallons, double pricePerGallon)
  150. {
  151. return gallons * pricePerGallon;
  152. }
  153.  
  154. // Calculate the total labor charges
  155. double calculateLaborCost(double laborHours)
  156. {
  157. return laborHours * hourlyLaborRate;
  158. }
Success #stdin #stdout 0.01s 5288KB
stdin
3 25
stdout
Enter the number of rooms to be painted: 
Enter the price of paint per gallon (at least $10.00): $
Enter the square feet of wall space in room 1: 
Enter the square feet of wall space in room 2: 
Enter the square feet of wall space in room 3: 

Gallons of paint required: 1.00
Hours of labor required: 8.00
Cost of paint: $25.00
Labor charges: $144.00
Total cost of paint job: $169.00