Array searching problem

I am having a problem printing out the results for my code, It is supposed to print out the largest and smallest value in the array, but keeps giving me a value not entered and I don't know why. Any information is appreciated.

Thanks

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
//This program lets the user enter 10 numbers and then display the highest and lowest number. 

#include <iostream>
#include<stdio.h>
using namespace std;

int main()
{
    const int NUM_ENTERED = 9;
    int number[NUM_ENTERED], highestValue = number[0], lowestValue = number[0], count=0;
    
    
    //Recieve the numbers
    cout << "Please enter the numbers:" << endl;
    cin >> number[0];
    cin >> number[1];
    cin >> number[2];
    cin >> number[3];
    cin >> number[4];
    cin >> number[5];
    cin >> number[6];
    cin >> number[7];
    cin >> number[8];
    cin >> number[9];

    //Runs a loop to decide which number is the HIGHEST and LOWEST number in the array       
    
    //I AM HAVING A PROBLEM WITH THE CODE HERE
    while (count < 10){
          
          if (number[count] > highestValue){
                            highestValue = number[count];
                            }
                            
          if (number[count] < lowestValue){
               lowestValue = number[count];
               }
          
          count ++;
          } //end loop

          
    //Prints the results 
    cout << "The highest number entered was: " << highestValue << endl;
    cout << "The lowest number entered was: " << lowestValue << endl;
    
    system("pause");
    return 0;
} //end main 
Your array has 9 elements, 0 to 8, so element 9 doesn't exist.

On line 10 you set the highest and lowest to the first element in the array which is uninitialized because user input is not until line 15.
Oh wow, I didn't even realize that. Thanks
Topic archived. No new replies allowed.