// readNums2.cc pconrad CISC181 02/10/06
// read numbers from a file and find the sum
// an improved version that can handle characters
// in the input

#include <iostream>
#include <fstream>
using namespace std;

#define BUFSIZE 1024

int main(void)
{
  // next line is "special"; we'll discuss on Monday @@@
  // it is called a "Constructor"
  // we won't see these in their full glory until Chapter 6
  
  ifstream infile ("nums.dat", ios::in);

  if (!infile)
    {
      cerr << "Could not open nums.dat, sorry!" << endl;
      exit (1);
    }
  
  // now read all nums from file
  // and find the sum

  char buffer[BUFSIZE];
  int x;
  int sum=0;

  infile.getline(buffer,BUFSIZE);
  while (!infile.eof()) // while not end of file
    {
      x = atoi(buffer);
      sum += x;
      infile.getline(buffer,BUFSIZE);
    }
  
  cout << "the sum is " << sum << endl;
  return 0;


}

