Don't know why it isn't working.

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
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
double average = 0.0;
int a = 0;
int main()
{
    int i = 0;
    double donation[10];
    for (int i=0; i<10; i++){
        cout << "Enter donation value number " << i + 1<< ": ";
        while ( !(cin >> donation[i]) ){ //checks for word input
            cin.clear();
                cout << "That's not a number";
                while  (cin.get() != '\n'){ //removes word input
                      continue;
                      }
                }
    }//from this point it stops working
               for (int x = 0; x < 10; x++){
                   average += donation[x];
                   }
                   for (int b = 0; b < 10; b++){
                     if (donation[b] > average){
                             cout << "Donation value number " << b << " is larger than 
the average,  $" << average/10 << "."; 
                             b++;   
                                     }
                   }
                     
        }
        



        
Last edited on
Once you get to the second for loop:
1
2
3
4
5
6
7
8
9
10
for (int x = 0; x < 10; x++){
    average += donation[x];
}
for (int b = 0; b < 10; b++){
    if (donation[b] > average){ //average is not average, it equals sum at this point
        cout << "Donation value number " << b << " is larger than 
the average,  $" << average/10 << "."; 
        b++;   
    }
}

You compare the donation to the "average" variable, but you forgot to take the average, so instead your comparing a single donation to the sum of all donations, so the if block will never execute.

You could fix this by simply taking the average before the loop:
1
2
3
4
5
6
7
8
9
10
11
for (int x = 0; x < 10; x++){
    average += donation[x];
}
average /= 10; //Take average before loop.
for (int b = 0; b < 10; b++){
    if (donation[b] > average){
        cout << "Donation value number " << b+1 << " is larger than 
the average,  $" << average << ".";  //Don't need to divide by 10 here anymore, and it should be b+1 to get the correct entry number
        b++;   //You increment b twice, remove this line if you want to print all donations greater than the average.
    }
}
Last edited on
Topic archived. No new replies allowed.