32767!?!?!?!

Hi this is my first time creating a C++ code. The task is to ask the user for ten numbers, determine whether each is even or odd and determine the max, min, and sum of the 10 numbers.

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
 
#include <iostream>
using namespace std;
int main ()
{
    int min, max, sum, n;
    int x = 1; //set a starting number for my loop
    
    sum = 0; //starting sum of 0
    min = n;
    max = n;
    
    while (x <= 10)  //loop will run 10 times
    {
        cout << "Please enter a number:" << endl;
        cin >> n;
        
        //enters if-else to determine whether n is even or odd
        
        if (n % 2 == 0)
        {
            cout << n << " is even." <<endl;
        }
        else
        {
            cout << n << " is odd." << endl;
        }
        
        //if statements to determine and set min and max
        
        if (n < min)
        {
            min = n;
        }
        if (n > max)
        {
            max = n;
        }
        
        sum = sum + n;
        x = x + 1;
        
    }
    
    cout << "The minimum is: " << min << endl;
    cout << "The maximum is: " << max << endl;
    cout << "The sum is: " << sum << endl;
    
    return 0;
}


Above is my code but for some reason I keep getting 32767 as my maximum no matter what.


Please Help!
Lines 10 and 11; you are assigning n to max and min, but what is the value of n? You haven't initialized it.
1
2
3
4
5
6
7
8
9
10
11
12
    int min, max, sum, n;  // at this point.. n is uninitialized.  So it can contain anything
    int x = 1;
    
    sum = 0;
    min = n;  // you assign n to min and max... but n is uninitialized.  This means min and
    max = n;  // max are now filled with random garbage

   // then on line 35
        if (n > max)  // if n > max... but what is max?  It's garbage!
        {
            max = n;
        }


What's happening is that your 'garbage' happens to be 32767... which means no number the user inputs is greater than it.
Initialize your max and min properly (lines 10 and 11). Put #include <limits> and set min and max like so:
1
2
min = std::numeric_limits<int>::max(); //this isn't a typo!
max = std::numeric_limits<int>::min(); //neither is this! 

The <int> part comes from the fact that max and min are declared as ints.

EDIT: The gist of this is that you initialize min to something that will be larger than any of your potential inputs. That way, your min variable is guaranteed to be updated at some point. If you initialized min to 0 but all the numbers you entered were greater than zero, then min would remain 0 even though that's not the minimum input. The same applies in reverse for the max value.
Last edited on
booradley60, unfortunately I can't apply limits. The program has to run for any number the user inputs.

Disch, I tried moving when I assigned a value to min and max (lines 16 and 17) but now they both come out as 10 when I use the numbers 1-10 to test it.

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

#include <iostream>
using namespace std;
int main ()
{
    int min, max, sum, n;
    int x = 1; //set a starting number for my loop
    
    sum = 0; //starting sum of 0
    
    while (x <= 10)  //loop will run 10 times
    {
        cout << "Please enter a number:" << endl;
        cin >> n;
        
        min = n;
        max = n;
        
        //enters if-else to determine whether n is even or odd
        
        if (n % 2 == 0)
        {
            cout << n << " is even." <<endl;
        }
        else
        {
            cout << n << " is odd." << endl;
        }
        
        //if statements to determine and set min and max
        
        if (n < min)
        {
            min = n;
        }
        if (n > max)
        {
            max = n;
        }
        
        sum = sum + n;
        x = x + 1;
        
    }
    
    cout << "The minimum is: " << min << endl;
    cout << "The maximum is: " << max << endl;
    cout << "The sum is: " << sum << endl;
    
    return 0;
}
vrivas wrote:
booradley60, unfortunately I can't apply limits. The program has to run for any number the user inputs.
The solution I suggested would satisfy this requirement.
Disch, I tried moving when I assigned a value to min and max (lines 16 and 17) but now they both come out as 10 when I use the numbers 1-10 to test it.


That's because now, you are setting min and max to 'n' every loop iteration. So they will always end up being equal to the last number the user inputs.

booradley's suggestion will work here.

Another approach could be to only assign min,max to 'n' on the first loop iteration.
Topic archived. No new replies allowed.