fork download
  1. // Marvyn Jesse De La Torre CS1A Chapter 7, pg. 444, #3
  2. //
  3. /****************************************************************************
  4.  * CALCULATE SALSA SALES
  5.  *
  6.  * This program accepts a number of jars sold for five
  7.  * types of salsa. It stores the sales in an array and displays the sales
  8.  * for each type, the total sales, and the highest and lowest selling types.
  9.  *
  10.  * INPUT
  11.  * jarsSold // Number of jars sold for each salsa type
  12.  *
  13.  * OUTPUT
  14.  * jarsSold // Sales for each type of salsa
  15.  * totalSales // Total number of jars sold
  16.  * highestType // Salsa type with the most jars sold
  17.  * lowestType // Salsa type with the fewest jars sold
  18.  ****************************************************************************/
  19.  
  20. #include <iostream>
  21. #include <string>
  22. #include <iomanip>
  23. #include <limits>
  24. using namespace std;
  25.  
  26. int main()
  27. {
  28. const int SIZE = 5;
  29.  
  30. // Store the five salsa names.
  31. string salsaNames[SIZE] =
  32. {
  33. "Mild", "Medium", "Sweet", "Hot", "Zesty"
  34. };
  35.  
  36. // Store the number of jars sold for each type.
  37. int jarsSold[SIZE];
  38. int totalSales = 0;
  39. int highestIndex = 0;
  40. int lowestIndex = 0;
  41.  
  42. // Get the sales amount for each salsa type.
  43. for (int i = 0; i < SIZE; i++)
  44. {
  45. cout << "Enter the number of jars sold for "
  46. << salsaNames[i] << " salsa: ";
  47. cin >> jarsSold[i];
  48.  
  49. // Do not accept negative sales amounts or non-integer input.
  50. while (cin.fail() || jarsSold[i] < 0)
  51. {
  52. cout << "Invalid amount. Enter zero or a positive number: ";
  53. cin.clear();
  54. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  55. cin >> jarsSold[i];
  56. }
  57.  
  58. // Add the current sales amount to the total.
  59. totalSales += jarsSold[i];
  60. }
  61.  
  62. // Find the highest and lowest selling salsa types.
  63. for (int i = 1; i < SIZE; i++)
  64. {
  65. if (jarsSold[i] > jarsSold[highestIndex])
  66. {
  67. highestIndex = i;
  68. }
  69.  
  70. if (jarsSold[i] < jarsSold[lowestIndex])
  71. {
  72. lowestIndex = i;
  73. }
  74. }
  75.  
  76. // Display the sales report.
  77. cout << "\nSalsa Sales Report\n";
  78. cout << "------------------\n";
  79. cout << left << setw(15) << "Salsa Type"
  80. << "Jars Sold\n";
  81.  
  82. for (int i = 0; i < SIZE; i++)
  83. {
  84. cout << left << setw(15) << salsaNames[i]
  85. << jarsSold[i] << endl;
  86. }
  87.  
  88. cout << "\nTotal jars sold: " << totalSales << endl;
  89. cout << "Highest selling salsa: " << salsaNames[highestIndex] << endl;
  90. cout << "Lowest selling salsa: " << salsaNames[lowestIndex] << endl;
  91.  
  92. return 0;
  93. }
Success #stdin #stdout 0.01s 5324KB
stdin
25
40
15
60
30
stdout
Enter the number of jars sold for Mild salsa: Enter the number of jars sold for Medium salsa: Enter the number of jars sold for Sweet salsa: Enter the number of jars sold for Hot salsa: Enter the number of jars sold for Zesty salsa: 
Salsa Sales Report
------------------
Salsa Type     Jars Sold
Mild           25
Medium         40
Sweet          15
Hot            60
Zesty          30

Total jars sold: 170
Highest selling salsa: Hot
Lowest selling salsa: Sweet