fork download
  1. //Cameron Pham CS1A Chapter 7, p. 444, #3
  2. //
  3. /******************************************************************************
  4. * TRACK SALES OF SALSA
  5. * ______________________________________________________________________________
  6. * This program accepts an integer value for the number of salsa jars sold. It
  7. * then determines the number of jars sold for each type of salsa and the total
  8. * number of jars sold. It determines the greatest amount of jars sold for one
  9. * type and the least amount of jars sold for one type.
  10. * ______________________________________________________________________________
  11. * INPUT
  12. * Jars // Number of jars sold
  13. * Salsa // Type of salsa being sold
  14. *
  15. * OUTPUT
  16. * Total // Total number of jars sold across all salsas
  17. * salsaSales // Number of jars sold for each salsa type
  18. * Greatest // The type of salsa with the most amount sold
  19. * Least // The type of salsa with the least amount sold
  20. *******************************************************************************/
  21. #include <iostream>
  22. #include <string>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. int main()
  27. {
  28. // Create an array containing the five salsa names.
  29. // The names are initialized when the array is created.
  30. string salsaNames[5] =
  31. {
  32. "Mild", "Medium", "Sweet", "Hot", "Zesty"
  33. };
  34.  
  35. // Create a parallel array to store the number of jars sold
  36. const int SIZE = 5;
  37. int jarsSold[SIZE];
  38. // Store the total number of jars sold
  39. int totalSales = 0;
  40. // Store the array positions of the highest and lowest sales
  41. int highestIndex = 0;
  42. int lowestIndex = 0;
  43. // Ask the user to enter the sales for each salsa type
  44. for (int i = 0; i < SIZE; i++)
  45. {
  46. cout << "Enter the number of jars sold for "
  47. << salsaNames[i] << " salsa: ";
  48. cin >> jarsSold[i];
  49.  
  50. // Validate the input so that sales cannot be negative
  51. while (jarsSold[i] < 0)
  52. {
  53. cout << "Sales cannot be negative. "
  54. << "Enter the number of jars sold: ";
  55. cin >> jarsSold[i];
  56. }
  57. // Add the current sales amount to the total
  58. totalSales += jarsSold[i];
  59. }
  60. // Find the highest and lowest selling salsa types
  61. for (int i = 1; i < SIZE; i++)
  62. {
  63. if (jarsSold[i] > jarsSold[highestIndex])
  64. {
  65. highestIndex = i;
  66. }
  67. if (jarsSold[i] < jarsSold[lowestIndex])
  68. {
  69. lowestIndex = i;
  70. }
  71. }
  72. // Display the sales report.
  73. cout << "\n--- Salsa Sales Report ---\n";
  74. cout << left << setw(20) << "Salsa Type"
  75. << setw(15) << "Jars Sold" << endl;
  76. cout << "-----------------------------------\n";
  77. for (int i = 0; i < SIZE; i++)
  78. {
  79. cout << left << setw(20) << salsaNames[i]
  80. << setw(15) << jarsSold[i] << endl;
  81. }
  82. // Display the total sales
  83. cout << "\nTotal jars sold: " << totalSales << endl;
  84. // Display the highest selling salsa
  85. cout << "Highest selling salsa: "
  86. << salsaNames[highestIndex] << endl;
  87. // Display the lowest selling salsa
  88. cout << "Lowest selling salsa: "
  89. << salsaNames[lowestIndex] << endl;
  90. return 0;
  91. }
Success #stdin #stdout 0s 5308KB
stdin
400 293 289 503 194
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                400            
Medium              293            
Sweet               289            
Hot                 503            
Zesty               194            

Total jars sold: 1679
Highest selling salsa: Hot
Lowest selling salsa: Zesty