questions... i'm a noob

Hey guys, I have a few questions with my code. Here is my code, along with a comment at the bottom with output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 #include <iostream>
using namespace std;

int main() {
    
    const int 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?

Thank you all so much for your help!
1
2
 int lowest = sales[0];
 int highest = sales[0];

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.
Last edited on
Awesome! I fixed both of those now, thank you :)

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        if(sales[i] = lowest)
        {
            lowestName = lowestName + " and " + sales[i];
        }
        if(sales[i] < lowest)
        {
            lowestName = "";
            lowest = sales[i];
            lowestName = salsa[i];
        }
        if(sales[i] = highest)
        {
            highestName = highestName + " and " + sales[i];
        }
        if(sales[i] > highest)
        {
            highestName = "";
            highest = sales[i];
            highestName = salsa[i];
        }
Last edited on
Topic archived. No new replies allowed.