    01: #include <string>
    02: #include <iostream>
    03: #include <fstream>
    04:
    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: {
    14:    double highest;
    15:    double next;
    16:    if (in >> next)
    17:       highest = next;
    18:    else
    19:       return 0;
    20:
    21:    while (in >> next)
    22:    {
    23:       if (next > highest)
    24:          highest = next;
    25:    }
    26:
    27:    return highest;
    28: }
    29:
    30: int main()
    31: {
    32:    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:    {
    40:       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:       {
    49:          cout << "Error opening " << filename << "\n";
    50:          return 1;
    51:       }
    52:
    53:       max = read_data(infile);
    54:       infile.close();
    55:    }
    56:    else
    57:       max = read_data(cin);
    58:
    59:    cout << "The maximum value is " << max << "\n";
    60:
    61:    return 0;
    62: }