// findAvg_alt.cc  P. Conrad    02.14.06  for CISC181
// a style you'll see, but P. Conrad doesn't prefer

// find the average of an input file

#include <iostream> // we are doing normal keyboard i/o 
#include <fstream>
using namespace std; // we'll talk about this later

#define INFILE_NAME "nums.dat"

int main(void)
{
  double avg=0, sum=0;
  int i=0;
  int x;

  ifstream infile(INFILE_NAME,ios::in);

  if (!infile)
    {
      cerr << "Sorry, I could not open "  INFILE_NAME << endl;
      return 1;
    }

  // this test in the while loop has two "side-effects"
  // the side effects are (1) gives x a different value
  // (2) changes the current spot in the file
  // P. Conrad has a philosophy that if tests and while
  // loop tests should just ask and answer a question,
  // not change the state of the world


  while (infile >> x) // P.Conrad doesn't like this.
    {
      
      sum = sum + x;
      i++;

    }
  avg=sum/i;


  cout << "The average is " << avg << endl;
  return 0;
}

