fork download
  1. // Marvyn De La Torre CS1A Chapter 6, P.369, #3
  2. /*************************************************************************
  3.  * DETERMINE WINNING DIVISION
  4.  * This program accepts quarterly sales amounts for the Northeast,
  5.  * Southeast, Northwest, and Southwest divisions. It determines and
  6.  * displays the division with the greatest sales amount.
  7.  *
  8.  * INPUT
  9.  * northeast : Sales for the Northeast division
  10.  * southeast : Sales for the Southeast division
  11.  * northwest : Sales for the Northwest division
  12.  * southwest : Sales for the Southwest division
  13.  *
  14.  * OUTPUT
  15.  * winningDivision : Name of the division with the greatest sales amount
  16.  * highestSales : The greatest sales amount
  17.  *************************************************************************/
  18.  
  19. #include <iomanip>
  20. #include <iostream>
  21. #include <limits>
  22. #include <string>
  23. using namespace std;
  24.  
  25. // Function Prototypes
  26. double getSales(string division);
  27. void findHighest(double northeast, double southeast, double northwest,
  28. double southwest);
  29.  
  30. int main()
  31. {
  32. // Declare Variables
  33. double northeast; // Sales for Northeast division
  34. double southeast; // Sales for Southeast division
  35. double northwest; // Sales for Northwest division
  36. double southwest; // Sales for Southwest division
  37.  
  38. // Get Sales Totals
  39. northeast = getSales("Northeast");
  40. southeast = getSales("Southeast");
  41. northwest = getSales("Northwest");
  42. southwest = getSales("Southwest");
  43.  
  44. // Display the Winning Division
  45. findHighest(northeast, southeast, northwest, southwest);
  46.  
  47. return 0;
  48. }
  49.  
  50. /*************************************************************************
  51.  * getSales
  52.  * This function asks for and validates one division's sales amount.
  53.  *************************************************************************/
  54. double getSales(string division)
  55. {
  56. double sales; // Sales amount entered by the user
  57.  
  58. cout << "Enter quarterly sales for the " << division << " division: $";
  59. cin >> sales;
  60. cout << endl;
  61.  
  62. // Validate Input
  63. while (cin.fail() || sales < 0)
  64. {
  65. cout << "Enter a dollar amount of $0.00 or greater: $";
  66. cin.clear();
  67. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  68. cin >> sales;
  69. }
  70.  
  71. return sales;
  72. }
  73.  
  74. /*************************************************************************
  75.  * findHighest
  76.  * This function determines and displays the division with the highest
  77.  * quarterly sales amount.
  78.  *************************************************************************/
  79. void findHighest(double northeast, double southeast, double northwest,
  80. double southwest)
  81. {
  82. double highestSales; // Largest sales amount
  83. string winningDivision; // Name of division with the largest sales amount
  84.  
  85. // Set Northeast as the starting highest value
  86. highestSales = northeast;
  87. winningDivision = "Northeast";
  88.  
  89. // Compare the remaining division totals
  90. if (southeast > highestSales)
  91. {
  92. highestSales = southeast;
  93. winningDivision = "Southeast";
  94. }
  95.  
  96. if (northwest > highestSales)
  97. {
  98. highestSales = northwest;
  99. winningDivision = "Northwest";
  100. }
  101.  
  102. if (southwest > highestSales)
  103. {
  104. highestSales = southwest;
  105. winningDivision = "Southwest";
  106. }
  107.  
  108. // Display Result
  109. cout << fixed << setprecision(2);
  110. cout << "\nThe high-grossing division is the " << winningDivision
  111. << " division." << endl;
  112. cout << "Its quarterly sales were $" << highestSales << "." << endl;
  113. }
Success #stdin #stdout 0s 5320KB
stdin
25000 
18000 
32000 
22000
stdout
Enter quarterly sales for the Northeast division: $
Enter quarterly sales for the Southeast division: $
Enter quarterly sales for the Northwest division: $
Enter quarterly sales for the Southwest division: $

The high-grossing division is the Northwest division.
Its quarterly sales were $32000.00.