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(ifstream& in) 13: { double highest; 15: double next; 16: if (in >> next) highest = next; 18: else return 0; 20: 21: while (in >> next) 22: if (next > highest) highest = next; 27: return highest; 28: } 29: 30: int main() 31: { string filename; 33: cout << "Please enter the data file name: "; 34: cin >> filename; 35: 36: ifstream infile; 37: infile.open(filename.c_str()); 38: 39: if (infile.fail())
40: { cout << "Error opening " << filename << "\n"; 42: return 1; 43: } 44: 45: double max = read_data(infile); 46: cout << "The maximum value is " << max << "\n"; 47: 48: infile.close(); 49: return 0; 50: }