/* This program processes meteorological data stored in a file named
   "pressure.dat".

   Input(File):  A sequence of meterological readings
   Output(File): The number of readings, the minimum reading,
                 the maximum reading, and the average reading
-----------------------------------------------------------------*/

#include <iostream.h>               // contains ios
#include <fstream.h>                // contains fstream
#include <stdlib.h>                 // contains exit()

int main(void)
{
   const char
      InputFileName[] = "pressure.dat",   // name of the input file
      OutputFileName[] = "pressure.out",  // name of the output file
      ErrorMsg[] = "\n*** main: unable to open file: ";

   cout << "\nThis program computes the number, maximum, minimum, and"
        << "\n\taverage of an input list of numbers in the file '"
        << InputFileName << "'\n\tand places its results in the file '"
        << OutputFileName << "'.\n";

   // ----------- Input Section -------------------------------------
   fstream
      InStream;                         // a stream to the input file

   InStream.open(InputFileName, ios::in);
                                        // establish the connection

   if (InStream.fail())                 // if stream cannot be opened
   {
      cerr << ErrorMsg << InputFileName //   display error msg, and
           << "\n\n";
      exit(-1);                         //   terminate abnormally
   }
                                        // otherwise declare
   int                                  //   and initialize vars:
      Count = 0;                        //    number of values
   double
      Reading,                          //    value being processed
      Sum = 0.0;                        //    running total

   InStream >> Reading;                 // read a value (from fstream)

   double
      Max = Reading,                    //    largest seen so far
      Min = Reading;                    //    smallest seen so far

   while (!InStream.eof())              // while there is more data
   {
      Count++;                          //   update: Count,
      Sum += Reading;                   //           Sum,
      if (Reading < Min)
         Min = Reading;                 //           Minimum, and
      else if (Reading > Max)
         Max = Reading;                 //           Maximum.
      InStream >> Reading;              //   read next value
                                        //   (from fstream)
   }                                    // end while

   InStream.close();                    // close the connection

   // ------------ Output Section ----------------------------------

   fstream
      OutStream(OutputFileName, ios::out);
                                        // open fstream for output
   if (OutStream.fail())                // if stream cannot be opened
   {
      cerr << ErrorMsg << OutputFileName//   display error message
           << "\n\n";
      exit (-1);                        //   and terminate.
   }                                    // otherwise:
                                        //   output results
                                        //   (to fstream)
   OutStream << "\n-->There were " << Count << " values.";

   if (Count > 0)
      OutStream << "\n\tranging from " << Min
                << " to " << Max
                << "\n\tand their average is " << Sum / Count
                << ".\n\n";

   return 0;
}
