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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;

int main(){
double x = 0;
int howmany = 0;
int anotherNumber;


do 
{

cout<<"Enter a number: "<<endl;
cin>>x;
howmany++;
cout<<"Are you finished? 2 = finished. "<<endl;
cin>>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
Yes, that's so much better than my solution. Sorry about the misleading answers!
It is compiling good, having problems with the coding though. Getting wrong averages : [
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;

int main(){
double x = 0, input;
int howmany = 0;
int anotherNumber;


do 
{

cout<<"Enter a number: "<<endl;
cin>>input;
howmany++;
cout<<"Are you finished? 2 = finished. "<<endl;
cin>>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.