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