Help with Do-while Conditions and Sorting

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.
Last edited on
in line 26 sum = sum + numarray[i] I believe should be sum = sum + tmp; Since you want the loop to add the positive integers.
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).
At line 31, the <= operator should be simply <

for (i = 0; i < entries; i++)
There's a similar problem at line 29.


Another comment. Not a serious problem, but you should make full use of SIZE.

cout << "Enter up to " << SIZE << " numbers. Enter a negative number when you are done." << endl;

} while (tmp >= 0 && entries < SIZE); note < sign rather than <=


Thank you Chervil. I have been focusing on the actual loop itself for so long that I didn't consider the problem may be outside of it.
Topic archived. No new replies allowed.