// main.cc P. Conrad, 4/5/05
// Test the Acct_C class, separate from building linked lists.
// In this program, we will just use an array of Acct_C objects.

// This version fixes the problem with the "taking address of bound function"
// error by inserting the () in the calls to member functions.

#include "acct.h"

#include <iostream>
#include <fstream>

using namespace std;

const int MAX_ACCTS = 10;

int main(int argc, char *argv[])
{

  if (argc!=2)
    {
      cerr << "Usage: " << argv[0] << " inputFile " << endl;
      return -1;
    }

  ifstream infile(argv[1],ios::in);

  if (!infile)
    {
      cerr << "Could not open " << argv[1] << endl;
      return -2;
    }

  int thisAcctNum;
  char thisName[NAME_MAX];

  Acct_C accounts[MAX_ACCTS]; // this line requires a default constructor

  int count = 0;

  infile >> thisAcctNum >> thisName;
  while (!infile.eof())
    {
      
      if (count == MAX_ACCTS)
	{
	  cerr << "Too many accounts in file... increase MAX_ACCTS and "
	       << "recompile the program." << endl;
	  return -3;
	}

      accounts[count].setName(thisName);
      accounts[count].setAcctNum(thisAcctNum);

      // increment count AFTER putting item in array.

      count++; // we got another item from the file successfully.


      
      // try to read the next line from the file
      infile >> thisAcctNum >> thisName;
    }
  
  cout << "The program read " << count << " lines from the file " << endl;
  cout << "Here they are: " << endl;

  for (int i=0; i<count; i++)
    cout << "\t" << accounts[i].getAcctNum()
	 << "\t" << accounts[i].getName() << endl;

  cout << endl;
  
  return 0;

}


