Loop fail

//Write a c++ program that will input double numbers for the user in a loop
// until the average of the numbers is less than 10. When the loop ends
// display the sum and the average of the numbers.

i tried but failed miserably to solve this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include<iostream>
using namespace std;
int main()
{
 int n, avg=10, sum=0, i;
 while (i <=avg)
 {
 cout<<"Enter a number";
 cin>>n;
 sum = sum+n;
 avg= sum/i;
 i++;
 }
 cout<<"sum ="<<sum<<endl;
 cout<<"avg = "<<avg;
    return 0;
}
Last edited on
Break it into parts

that will input double numbers

if you mean to ask user for two numbers, you didn't do this. Should look like
cin >> a >> b; This will work for space-separated user input.

in a loop until the average of the numbers is less than 10

should be while (avg >= 10.0)

Your average variable should probably be of type double or float. Do you know about integer division?

Rest you can prob figure out.
Topic archived. No new replies allowed.