01: #include <string>
02: #include <iostream>
03: #include <fstream>
05: using namespace std;
06: 
07: /**
08:    Reads numbers from a file and finds the maximum value
09:    @param in the input stream to read from
10:    @return the maximum value or 0 if the file has no numbers
11: */            
12: double read_data(istream& in) 13: { double highest; 15: double next; 16: if (in >> next) highest = next; 18: else return 0; 20: 21: while (in >> next)
23: if (next > highest) highest = next; 27: return highest; 28: } 29: 30: int main() 31: { double max; 33: 34: string input; 35: cout << "Do you want to read from a file? (y/n) "; 36: cin >> input; 37: 38: if (input == "y") 39: { string filename; 41: cout << "Please enter the data file name: "; 42: cin >> filename; 43: 44: ifstream infile; 45: infile.open(filename.c_str()); 46: 47: if (infile.fail())
48: { cout << "Error opening " << filename << "\n"; 50: return 1; 51: } 52: 53: max = read_data(infile); 54: infile.close(); 55: } 56: else max = read_data(cin); 58: 59: cout << "The maximum value is " << max << "\n"; 61: return 0; 62: }