Program: finds average of numbers user inputs

Creating a program that finds that average after a user inputs any amount of numbers. The program finds the average of the numbers when a user inputs a negative number (average does not include the negative number). My program is only displaying the combined total of the numbers input including the negative number. Could use some help as to where I've gone wrong.

Here is my code:

http://pastebin.com/NHkXSwxJ
You only increment the Counter once at the beginning of your code, not during any of the looping. So at the end the sum is just divided by 1.

Get rid of your counter++; on line 8 as there is no need for it and change your code to something like this instead. (It doesn't have be done exactly this way though).

1
2
3
4
5
6
7
8
9
10
11
12
do
	{
		cin >> input;

		if(input < 0)
		{
			break; // Breaks out of the loop if the number is less than 0
		}

		counter++;      // Increments counter by 1
		sum += input; // Adds to sum 
	}
Check this thread if you like. I posted a similar solution. You should only have to tweak it slightly to get the results you need.

http://www.cplusplus.com/forum/beginner/74275/


EDIT - You can copy and paste your source code right in your post and wrap it with CODE blocks. Just highlight the code and click the <> button on the right-hand side.
Last edited on
Topic archived. No new replies allowed.