I'm very new to programming and ive been trying to figure out this problem for a few days and still have no luck, I cant figure out how to get the average to ignore an out of range number. Also the range is the max- the min which i cant get to work either please help. Code: #include <iostream> using namespace std; int main() { double x; double sum = 0.0; int number = 0; double average; double max = 0; double min = 0; while (cin>>x) { if ((x < 0) || (x > 100)) { cin.ignore(3,'\n'); cout << "Out of range ; ignored." << endl; } if (x > max) max = x; if (min > x) x = min; sum += x; number++; } if (number > 0) { average = sum / number; cout<<"The average is "<< average; cout<<" The range is "<< max - min; cout << endl; } return 0; }
You're starting min at 0, so nothing will be less than it. You need to start it at the maximum (100). You need to add "continue;" after the out of range check to skip the rest of the while loop: Code: if ((x < 0) || (x > 100)) { cin.ignore(99,'\n'); cout << "Out of range ; ignored." << endl; continue; } The line x = min needs to be min = x.