// readNums1.cc 
// read Numbers from a file 
//   a more robust version... this one will ignore any lines that contain
//     character input at the start, or any characters after a valid integer
//     on a line
// P. Conrad 02/09/06


#include <iostream>
#include <fstream> // for reading from files, or writing to files
using namespace std;

int main(void)
{

  ifstream infile("nums.dat", ios::in);

  char buffer[1024]; // a nice big array of characters

  if (!infile)
    {
      cerr << "I could not open nums.dat... sorry!" << endl;
      return 1; // indicates an error condition
    }

  int sum = 0;
  
  int x;

  infile.getline(buffer,1024); // read an entire line of text into C string
  while(!infile.eof())
    {
      x =  atoi(buffer); // atoi is "ASCII to integer"
      sum += x;
      infile.getline(buffer,1024); // read an entire line of text into C string
    }

  cout << "sum = " << sum << endl;

  return 0;
}

