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!
Please don't post the same question multiple times.
http://www.cplusplus.com/forum/general/130073/
1
2
    min = n;
    max = n;
n is uninitialized so this is undefined behavior. You should set the min to a large number (or the first number being input) and then set max to a small number (or the first number being input).
Last edited on
Topic archived. No new replies allowed.