Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Help with simple program (http://www.go4expert.com/forums/help-simple-program-t14591/)

 Steve18 17Oct2008 06:54

Help with simple program

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; }```

 asha 17Oct2008 10:34

Re: Help with simple program

What's the objective of the program??

 oogabooga 17Oct2008 21:56

Re: Help with simple program

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.

 All times are GMT +5.5. The time now is 06:47.