#include <iostream>
#include <string>
#include <cctype>
usingnamespace 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++;
}
}
}
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.
}
}