A few issues then, you have two variables: n, and num. What job is each variable supposed to be doing?
It looks like n is supposed to keep track of the number of numbers you have entered, and num is the current number being entered. The "else n++" part of your code I don't think is necessary, I think it would make more sense to do
1 2 3 4 5 6
|
cin >> n;
if (num > 0)
{
avg += num;
n++;
}
|
with your while condition being
while (num > 0)
Also, don't have the cout << "Average is" << ... line
inside the loop: Place that after the loop ends.
And don't forget to initialize your variables before your loop!
Initialize n to be 0, num to some positive number, and initialize avg to be 0!
One more thing:
avg/n is doing integer division: This cuts off any decimal places that would happen.
Try seeing if your results are different if you do this:
"Average is " << static_cast<double>(avg)/n
This makes floating-point division happen instead.
Edit:
+= num
instead of
+= n
.
Edit 2: Some other typos I made, sorry, you probably want to re-read my post