fork download
  1. // Elaine Torrez CS1A Chapter 6, P.369, #3
  2. // *****************************************************************************************
  3. // * WINNING DIVISION *
  4. // *--------------------------------------------------------------------------------------- *
  5. // * This program determines which of a company's four divisions (Northeast, Southeast, *
  6. // * Northwest, and Southwest) had the greatest quarterly sales. *
  7. // * It uses two functions: *
  8. // * getSales() – asks for a division’s sales and validates input *
  9. // * findHighest()– determines and displays which division had the highest sales *
  10. // *--------------------------------------------------------------------------------------- *
  11. // * INPUT *
  12. // * Northeast : quarterly sales for Northeast division (>= 0) *
  13. // * Southeast : quarterly sales for Southeast division (>= 0) *
  14. // * Northwest : quarterly sales for Northwest division (>= 0) *
  15. // * Southwest : quarterly sales for Southwest division (>= 0) *
  16. // *--------------------------------------------------------------------------------------- *
  17. // * OUTPUT *
  18. // * Name of the highest-grossing division and its sales amount *
  19. // *****************************************************************************************
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23. #include <string>
  24. #include <limits> // for input validation
  25. using namespace std;
  26.  
  27. // ---------------- Function Prototypes ----------------
  28. double getSales(string divisionName);
  29. void findHighest(double northeast, double southeast, double northwest, double southwest);
  30.  
  31. // ---------------------- MAIN -------------------------
  32. int main()
  33. {
  34. double northeast, southeast, northwest, southwest;
  35.  
  36. cout << fixed << setprecision(2);
  37.  
  38. // Get sales for each division
  39. northeast = getSales("Northeast");
  40. southeast = getSales("Southeast");
  41. northwest = getSales("Northwest");
  42. southwest = getSales("Southwest");
  43.  
  44. // Determine and display highest
  45. findHighest(northeast, southeast, northwest, southwest);
  46.  
  47. return 0;
  48. }
  49.  
  50. // ---------------- Function Definitions ----------------
  51.  
  52. // Function to get a division’s sales
  53. double getSales(string divisionName)
  54. {
  55. double sales;
  56. cout << "Enter the quarterly sales for the " << divisionName << " division: $";
  57. cin >> sales;
  58.  
  59. // Input validation
  60. while (cin.fail() || sales < 0.0)
  61. {
  62. cin.clear();
  63. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  64. cout << "ERROR: Sales amount must be 0 or greater. Re-enter for "
  65. << divisionName << ": $";
  66. cin >> sales;
  67. }
  68.  
  69. return sales;
  70. }
  71.  
  72. // Function to find and display which division had the highest sales
  73. void findHighest(double northeast, double southeast, double northwest, double southwest)
  74. {
  75. double highest = northeast;
  76. string division = "Northeast";
  77.  
  78. if (southeast > highest)
  79. {
  80. highest = southeast;
  81. division = "Southeast";
  82. }
  83. if (northwest > highest)
  84. {
  85. highest = northwest;
  86. division = "Northwest";
  87. }
  88. if (southwest > highest)
  89. {
  90. highest = southwest;
  91. division = "Southwest";
  92. }
  93.  
  94. cout << "\nThe highest-grossing division is the " << division << " division.\n";
  95. cout << "Sales amount: $" << highest << endl;
  96. }
  97.  
Success #stdin #stdout 0s 5316KB
stdin
12000
18500
16400
19500
stdout
Enter the quarterly sales for the Northeast division: $Enter the quarterly sales for the Southeast division: $Enter the quarterly sales for the Northwest division: $Enter the quarterly sales for the Southwest division: $
The highest-grossing division is the Southwest division.
Sales amount: $19500.00