fork download
  1. //Sam Partovi CS1A Ch. 11, P.645, #3
  2. /*******************************************************************************
  3. * STORE CORPORATE SALES DATA
  4. * ____________________________________________________________
  5. * This program accepts corporate sales data and stores it, calculating averages
  6. * where necessary.
  7. * ____________________________________________________________
  8. *INPUT
  9. * firstQuarterSales : Sales in first quarter
  10. * secondQuarterSales : Sales in second quarter
  11. * thirdQuarterSales : Sales in third quarter
  12. * fourthQuarterSales : Sales in fourth quarter
  13. *OUTPUT
  14. * totalAnnualSales : Accumulated yearly sales
  15. * averageQuarterlySales : Average of quarterly sales
  16. *******************************************************************************/
  17. #include <iostream>
  18. #include <string>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. // Structure to hold division sales data
  23. struct Division {
  24. string name;
  25. double firstQuarterSales;
  26. double secondQuarterSales;
  27. double thirdQuarterSales;
  28. double fourthQuarterSales;
  29. double totalAnnualSales;
  30. double averageQuarterlySales;
  31. };
  32.  
  33. // Function prototypes
  34. void getSalesData(Division& division);
  35. void calculateSales(Division& division);
  36. void displayDivisionData(const Division& division);
  37. void validateSales(double& sales);
  38.  
  39. int main() {
  40. // Create four division variables
  41. Division east = {"East"};
  42. Division west = {"West"};
  43. Division north = {"North"};
  44. Division south = {"South"};
  45.  
  46. // Input sales data for each division
  47. cout << "\nEnter sales data for the East division:";
  48. getSalesData(east);
  49. cout << "\nEnter sales data for the West division:";
  50. getSalesData(west);
  51. cout << "\nEnter sales data for the North division:";
  52. getSalesData(north);
  53. cout << "\nEnter sales data for the South division:";
  54. getSalesData(south);
  55.  
  56. // Calculate totals and averages
  57. calculateSales(east);
  58. calculateSales(west);
  59. calculateSales(north);
  60. calculateSales(south);
  61.  
  62. // Display sales data
  63. cout << fixed << setprecision(2);
  64. cout << "\nCorporate Sales Data\n";
  65. displayDivisionData(east);
  66. displayDivisionData(west);
  67. displayDivisionData(north);
  68. displayDivisionData(south);
  69.  
  70. return 0;
  71. }
  72.  
  73. /*******************************************************************************
  74.  * getSalesData
  75.  * Prompts the user to input the sales data for each quarter of a division.
  76.  ******************************************************************************/
  77. void getSalesData(Division& division) {
  78. cout << "\nEnter sales for the first quarter: ";
  79. cin >> division.firstQuarterSales;
  80. validateSales(division.firstQuarterSales);
  81.  
  82. cout << "\nEnter sales for the second quarter: ";
  83. cin >> division.secondQuarterSales;
  84. validateSales(division.secondQuarterSales);
  85.  
  86. cout << "\nEnter sales for the third quarter: ";
  87. cin >> division.thirdQuarterSales;
  88. validateSales(division.thirdQuarterSales);
  89.  
  90. cout << "\nEnter sales for the fourth quarter: ";
  91. cin >> division.fourthQuarterSales;
  92. validateSales(division.fourthQuarterSales);
  93. }
  94.  
  95. /*******************************************************************************
  96.  * calculateSales
  97.  * Calculates the total annual sales and average quarterly sales for a division.
  98.  ******************************************************************************/
  99. void calculateSales(Division& division) {
  100. division.totalAnnualSales = division.firstQuarterSales
  101. + division.secondQuarterSales + division.thirdQuarterSales
  102. + division.fourthQuarterSales;
  103. division.averageQuarterlySales = division.totalAnnualSales / 4.0;
  104. }
  105.  
  106. /*******************************************************************************
  107.  * displayDivisionData
  108.  * Displays the sales data for a division.
  109.  ******************************************************************************/
  110. void displayDivisionData(const Division& division) {
  111. cout << "\nDivision: " << division.name << "\n";
  112. cout << " First Quarter Sales: $" << division.firstQuarterSales << "\n";
  113. cout << " Second Quarter Sales: $" << division.secondQuarterSales << "\n";
  114. cout << " Third Quarter Sales: $" << division.thirdQuarterSales << "\n";
  115. cout << " Fourth Quarter Sales: $" << division.fourthQuarterSales << "\n";
  116. cout << " Total Annual Sales: $" << division.totalAnnualSales << "\n";
  117. cout << " Average Quarterly Sales: $" << division.averageQuarterlySales
  118. << "\n";
  119. }
  120.  
  121. /*******************************************************************************
  122.  * validateSales
  123.  * Ensures that the sales value is non-negative.
  124.  *******************************************************************************/
  125. void validateSales(double& sales) {
  126. while (sales < 0) {
  127. cout << "Invalid sales figure. Sales cannot be negative. Please "
  128. "re-enter: ";
  129. cin >> sales;
  130. }
  131. }
  132.  
Success #stdin #stdout 0.01s 5280KB
stdin
5000
7000
8000
6500
4500
6200
7500
7000
5200
8100
7600
6200
5800
6900
7200
6300
stdout
Enter sales data for the East division:
Enter sales for the first quarter: 
Enter sales for the second quarter: 
Enter sales for the third quarter: 
Enter sales for the fourth quarter: 
Enter sales data for the West division:
Enter sales for the first quarter: 
Enter sales for the second quarter: 
Enter sales for the third quarter: 
Enter sales for the fourth quarter: 
Enter sales data for the North division:
Enter sales for the first quarter: 
Enter sales for the second quarter: 
Enter sales for the third quarter: 
Enter sales for the fourth quarter: 
Enter sales data for the South division:
Enter sales for the first quarter: 
Enter sales for the second quarter: 
Enter sales for the third quarter: 
Enter sales for the fourth quarter: 
Corporate Sales Data

Division: East
  First Quarter Sales: $5000.00
  Second Quarter Sales: $7000.00
  Third Quarter Sales: $8000.00
  Fourth Quarter Sales: $6500.00
  Total Annual Sales: $26500.00
  Average Quarterly Sales: $6625.00

Division: West
  First Quarter Sales: $4500.00
  Second Quarter Sales: $6200.00
  Third Quarter Sales: $7500.00
  Fourth Quarter Sales: $7000.00
  Total Annual Sales: $25200.00
  Average Quarterly Sales: $6300.00

Division: North
  First Quarter Sales: $5200.00
  Second Quarter Sales: $8100.00
  Third Quarter Sales: $7600.00
  Fourth Quarter Sales: $6200.00
  Total Annual Sales: $27100.00
  Average Quarterly Sales: $6775.00

Division: South
  First Quarter Sales: $5800.00
  Second Quarter Sales: $6900.00
  Third Quarter Sales: $7200.00
  Fourth Quarter Sales: $6300.00
  Total Annual Sales: $26200.00
  Average Quarterly Sales: $6550.00