#include #include #include #include "CPstring.h" // file: stats4.cc; author: D. Ramm; date: 10/13/96 // file input; while with EOF to exit // initialization from limits.h // move stats to function with reference parameters void Stats(int number, int & max, int & min, int & sum, int & count) // Precondition: number is any integer data value, max, min, // sum, and count are previous best values for maximum, minimum // total sum, and count to numbers encountered. // Postcondition: max, min, sum, and count have been updated // appropriately. { count++; sum += number; if (number > max) { max = number; } if (number < min) { min = number; } } int main() { int number, count = 0; int sum = 0, max = INT_MIN, min = INT_MAX; string filename; ifstream filein; cout << "Enter file name: "; cin >> filename; filein.open(filename); while (filein >> number) { Stats(number, max, min, sum, count); cout << "Do something with number: " << number << endl; } if (count == 0) cout << "No data found in file " << filename << endl; else cout << "For " << count << " numbers read, Max = " << max << ", Min = " << min << ", Mean = " << 1.0*sum/count << endl; return 0; } Sample output: stats4 Enter file name: data Do something with number: 1 Do something with number: 2 Do something with number: 4 Do something with number: 7 Do something with number: -2 Do something with number: 33 Do something with number: -11 Do something with number: 12 For 8 numbers read, Max = 33, Min = -11, Mean = 5.75