#include <iostream>
usingnamespace std;
int main() {
constint NUM = 5;
string salsa[NUM] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
int sales[NUM];
int total = 0;
string lowestName;
string highestName;
int lowest = sales[0];
int highest = sales[0];
double average;
for(int i=0; i<NUM; i++)
{
cout << "Enter the number of sales this month for " << salsa[i] << ": ";
cin >> sales[i];
if(sales[i] < 0)
{
cout << "Invalid answer. Please enter a different number: ";
cin >> sales[i];
}
total = total + sales[i];
if(sales[i] < lowest)
{
lowest = sales[i];
lowestName = salsa[i];
}
if(sales[i] > highest)
{
highest = sales[i];
highestName = salsa[i];
}
}
average = total/NUM;
cout << "Total sales: " << endl;
for(int x=0; x<NUM; x++)
{
cout << salsa[x] << " sold " << sales[x] << " jars this month." << endl;
}
cout << "The lowest selling type was " << lowestName << " with " << lowest << " jars sold." << endl;
cout << "The highest selling type was " << highestName << " with " << highest << " jars sold." << endl;
cout << "The total amount of jars sold was " << total << ".";
cout << "The average amount of jars sold was " << average << ".";
return 0;
}
/*
Enter the number of sales this month for Mild: 15
Enter the number of sales this month for Medium: 10
Enter the number of sales this month for Sweet: 8
Enter the number of sales this month for Hot: 12
Enter the number of sales this month for Zesty: -9
Invalid answer. Please enter a different number: 18
Total sales:
Mild sold 15 jars this month.
Medium sold 10 jars this month.
Sweet sold 8 jars this month.
Hot sold 12 jars this month.
Zesty sold 18 jars this month.
The lowest selling type was Sweet with 8 jars sold.
The highest selling type was with 1606416168 jars sold.
The total amount of jars sold was 63.The average amount of jars sold was 12.Program ended with exit code: 0
*/
1. Why does the "highest" not work?
2. Why does the "average" not work?
3. How can I set lowest and highest to recognize if there are multiple with the same number?
What happens if, at this point, sales[0] is really really high or really low? You are setting them to an unknown number at the start, and then comparing against them. What if, by chance, you've set them to a million, or to minus a million... or to 1606416168?
average = total/NUM;
int divided by int gives an int. So 3/4 equals 0. 10/3 equals 3. 8/5 equals 1. And 63/5 equals... 12.
If you want the result to be something with more precision than an int, turn one of them into a double or float before doing the division.
I'm still trying to figure out the highest and lowest with multiple values part. Here's what I'm trying to do. However, you can't use a + to add characters to a string I guess. How do I need to change this code?