/* meteorology1.cpp reads meteorological data stored in a file, 
 * computes the minimum, maximum, and average of the readings, 
 * and writes these statistics to an output file.
 *
 *  Input(keyboard): names of the input and output files
 *  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>                 // cin, cout
#include <fstream.h>                  // ifstream, ofstream
#include <string>                     // string, getline()
#include <assert.h>                   // assert()
#include <float.h>                    // DBL_MIN and DBL_MAX

int main()
{
   cout << "This program computes the number, maximum, minimum, and\n"
        << "average of an input list of numbers in one file,\n"
        << "and places its results in another file.\n\n";

   // ----------- Input Section -------------------------------------

   cout << "Enter the name of the input file: ";
   string inputFileName;
   getline(cin, inputFileName);              // get name of input file
                                             // open an input stream to 
   ifstream inStream;                        //  the input file,
   inStream.open(inputFileName.data());      //  establish connection,
   assert(inStream.is_open());               //  check for success

   int count = 0;                            // number of values
   double reading,                           // value being processed
          maximum = DBL_MIN,                 // largest seen so far
          minimum = DBL_MAX,                 // smallest seen so far
          sum = 0.0;                         // running total 

   for (;;)                                  // loop:
   {
      inStream >> reading;                   //   read a value

      if (inStream.eof()) break;             //   if eof, quit

      count++;                               //   update: count,
      sum += reading;                        //           sum,
      if (reading < minimum)
         minimum = reading;                  //           minimum, and
      if (reading > maximum)
         maximum = reading;                  //           maximum.
   }                                         // end loop

   inStream.close();                         // close the connection

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

   cout << "Enter the name of the output file: ";
   string outputFileName;
   getline(cin, outputFileName);
                                             // open an output stream to
   ofstream outStream(outputFileName.data());//  the output file,
                                             //  establish connection,
   assert(outStream.is_open());              //  and check for success
                                             // write results to file
   outStream << "\n--> There were " << count << " values.";

   if (count > 0)
      outStream << "\n\tranging from " << minimum 
                << " to " << maximum
                << "\n\tand their average is " << sum / count 
                << endl;

   outStream.close();                        // close the stream

   cout << "Processing complete.\n";

   return 0;
}


