You really need to be consistent with indenting and spacing. Also, as a newcomer to the language, it's a good idea to put bracket around *every* condition. Here is a cleaned-up version of the code with comments on the remaining problems:
Code:#include <iostream> using namespace std; int main() { double n, count=0, sum=0, min=0, // No, set high. max=100; // No, set low. for (;;) { if (cin.eof()) { break; } while (cin >> n) { if (n < min) { min = n; } /* This is wrong - doesn't depend on previous comparison. */ else if (n > max) { max = n; } if (n < 0 || n > 100) { cout << "out of range; ignored." << endl; } else { ++count; sum += n; /* This is wrong - calculation needed outside of loop. Also, 'range' is undeclared. */ range = max-min; } } } /* Note: cast is unnecessary. */ cout << "The average is " << float (sum)/count << endl; cout << "The range is " << range << endl; }



LinkBack URL
About LinkBacks



