### C++ Finding Average Of User Inputs Help

I need help finding the average of user inputs BUT, The user does not need to know how many numbers s/he has to average. This my code so far, what do I need to do to make it right?
 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main(){ int i = 0; double x = 0; int howmany = 0; int input = 0; cout<<"how many numbers do you have" << endl; cin >> howmany; while(i < howmany){ cout << " enter numbers" << endl; cin >> input; x = x + input; i++; } x = x/howmany; cout << "The average is " << x << endl; }``````

In other words find the average without the user having to input how many numbers they input-ed

I really hope someone can help me.
Last edited on
Can anyone help ?
How would you define your problem?
Write a program that allows the user to average as many numbers (double floating point) as the user desires and repeat as many times as the user wants to calculate averages.

The program needs to ask the user to enter the numbers to be averaged. The user DOES NOT need to know how many numbers s/he has to average.

The program output must echo the userâ€™s inputs and then show the user the average. The numerical output must always show the decimal point and provide two digits to the right of the decimal point, even if they are zeros.
I see... I didn't consider the part on the user not knowing.
I guess the best whay is to use a vector<int> to store the numbers (you don't have to worry about running out of memory) and a centinel to finish the input (say... input the numbers and type -1 to finish).

You don't have to worry about taking count of numbers, cause vectors have a method:

your_vector.length will give you the amount of numbers.
 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int main(){ double x = 0; int howmany = 0; int anotherNumber; do { cout<<"Enter a number: "<>x; howmany++; cout<<"Are you finished? 2 = finished. "<>anotherNumber; } while (anotherNumber!=2); x = x/howmany; cout << "The average is " << x << endl; } ``````

I can't compile it right now but I think that would work.
Last edited on
It is compiling good, having problems with the coding though. Getting wrong averages : [
 ``123456789101112131415161718192021222324252627282930`` ``````#include using namespace std; int main(){ double x = 0, input; int howmany = 0; int anotherNumber; do { cout<<"Enter a number: "<>input; howmany++; cout<<"Are you finished? 2 = finished. "<>anotherNumber; } while (anotherNumber!=2); x += input/howmany; cout << "The average is " << x << endl; } ``````

That should work.
Last edited on
still getting wrong averages.
I feel like its just one little thing thats making the average come out messed up.
thank you guys for the help.
Topic archived. No new replies allowed.