Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2015
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5.  
  6. // Prototypes
  7. int getSalesData(string[], int[]);
  8. int posOfLargest(int[]);
  9. int posOfSmallest(int[]);
  10. void displayReport(string[], int[], int);
  11.  
  12. const int SIZE = 5; //Sets a constant, SIZE, to 5. This is then used in the arrays, and sets them all to have 5 columns.
  13.  
  14. int main()
  15. {
  16. string name[SIZE] = { "Mild ", "Medium", "Sweet", "Hot", "Zesty" }; //Different types of salsa
  17. int sales[SIZE]; //How many were sold
  18.  
  19. int totalJarsSold = getSalesData(name, sales);
  20.  
  21. displayReport(name, sales, totalJarsSold); //Calls the displayReport function
  22.  
  23. return 0;
  24. }
  25.  
  26. int getSalesData(string name[], int sales[])
  27. {
  28. int total = 0; //Total jars sold
  29.  
  30. for (int i = 0; i < SIZE; i++)
  31. {
  32. cout << "Jars sold last month of " << name[i] << ": ";
  33. cin >> sales[i];
  34.  
  35. while (sales[i] < 0) //Input validation. If a number entered is less than 0, re enter
  36. {
  37. cout << "Jars sold must be 0 or more. Please enter again: ";
  38. cin >> sales[i];
  39. }
  40. total += sales[i]; //Adds all the salsas sold together in a variable
  41. }
  42. return total;
  43. }
  44.  
  45. void displayReport(string name[], int sales[], int total)
  46. {
  47. int mostJarsSold = posOfLargest(sales); //Calls posOfLargest, which sorts them largest to smallest.
  48. int leastJarsSold = posOfSmallest(sales); //Calls posOfSmallest, which sorts them smallest to largest.
  49.  
  50. cout << "Salsa Sales Report" << endl;
  51. cout << "Name Jars Sold " << endl; //Menu system
  52. cout << "__________________" << endl;
  53.  
  54. for (int i = 0; i < SIZE; i++)
  55. cout << name[i] << setw(11) << sales[i] << endl;
  56.  
  57. cout << "Total sales: " << total << endl; //Menu system
  58. cout << "Best seller: " << name[mostJarsSold] << endl; //More menu systems
  59. cout << "Least seller: " << name[leastJarsSold] << endl; //Even more menu systems
  60.  
  61. }
  62.  
  63. int posOfLargest(int array[])
  64. {
  65. int indexOfLargest = 0;
  66.  
  67. for (int i = 0; i < SIZE; i++)
  68. {
  69. if (array[i] > array[indexOfLargest]) //Sorts them largest to smallest
  70. indexOfLargest = i;
  71. }
  72. return indexOfLargest;
  73. }
  74.  
  75. int posOfSmallest(int array[])
  76. {
  77. int indexOfSmallest = 0;
  78.  
  79. for (int i = 0; i < SIZE; i++)
  80. {
  81. if (array[i] < array[indexOfSmallest]) //Sorts them smallest to largest
  82. indexOfSmallest = i;
  83. }
  84. return indexOfSmallest;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement