Hello all, the program I'm attempting to make a program that asks users to enter up to 100 numerical inputs, when a negative number is entered, the loop will end and the program will sort the numbers. I don't understand why the program is storing the negative number. When printed to the screen, the negative number displays as a large negative number for example: (-881923183) after all the regular inputs. Any help is appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
do {// takes in numbers into the array
tmp = 0;
cout << "Enter up to 100 numbers. Enter a negative number when you are done." << endl;
cin >> tmp;
if (tmp >= 0) {//Sets condition so negative numbers are not included in the average
numarray[i++] = tmp; //stores tmp into the array
entries++; //increments for the number of entries into the array
sum = sum + numarray[i]; //adds up elements in the array and stores as sum
}
} while (tmp >= 0 && entries <= 100); //exits loop when a negative is entered, or when 100 numbers are entered
for (i = 0; i <= entries; i++) {
cout << numarray[i] << endl;
}
I'm also having issues with my sorting algorithm, however, I will post that code once I resolve this issue.
Thank you Psalazar. That actually helped my average print out correctly. However, when I ask the program to print the numbers to the screen, the output still shows that negative number. For example if I enter the numbers 10, 10, and -1 (to exit the loop). The output shows:
10
10
-858993460
(It does print out the correct average now, however).