# Thread: User input how many times to run average

1. ## User input how many times to run average

Hi,

This is first class in C++ and I'm having logical calculation problem as below.

User needs to input how many times to run average, for example 6. User is prompted to enter six inputs, and he/she enters "0" for average to be computed. This does not give correct result as you can see below. I also would NOT like to limit the "while (count <= 10) " but it does not work if I don't input anything. Also if I use "MAXNUMS = 10" it also limits user to ten.

Anyway - regardless of while loop I'm not getting a correct answer.

I would appreaciate if someone can be able to point where Im going wrong. Im suspecting my while loop is not correct. Please help.

=====
Code:
```,
#include <iostream.h>
#include <iomanip.h>

int main()
{

//const int MAXNUMS = 0;
int count;
float num, nu_avg, average, total;

total = 0.0;
count = 0;

cout << endl << endl // Make some space

<< endl << "\nPlease type in the total number of data values to be averaged"
<< endl << " Enter a zero (0) when you have finished entering numbers and want to compute the"
<< endl << " average of the inputted numbers: " ;

cin >> nu_avg;

//cout << "\nPlease type in the total number of data values to be averaged:  "
// << " numbers.\n";
//<< numbers << " numbers.\n";
count ++;

count = 0;
//total = 0;

while (count <= 10)
{

count ++;

cout << "\nEnter a number: ";

cin  >> num;
total = total + num;
average = total / count;
cout << "The average is now "<< average;
//count++;
}

cout << "\nThe final average is " << average << endl;

return 0;
}```
------
output
-------
Please type in the total number of data values to be averaged
Enter a zero (0) when you have finished entering numbers and want to compute th
e
average of the inputted numbers: 4

Enter a number: 5
The average is now 5
Enter a number: 6
The average is now 5.5
Enter a number: 6
The average is now 5.66667
Enter a number: 5
The average is now 5.5
Enter a number: 4
The average is now 5.2
Enter a number: 0

The average is now 4.33333 // Wrong answer !!!

2. Why does your while loop go to 10 when they told you what number to stop at the first time around? For that matter, what's the point of the 0 when they told you what number to stop at?

3. Number "10" is just something I was trying to experiment. Even count has no given number, just experimenting. Keyboards reads the count once user types in.