// 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 program won't compile... why?

#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]  = {0}; // this line invokes NO constructor.
  // the {0} is used to initialize all elements of the array to NULL (0).

  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;
	}
      
      // invoke the constructor.  
      accounts[count] = new Acct_C(thisAcctNum,thisName);
      count++; // we got another item from the file successfully.

      // Note: accounts[count] is an address; the entire accounts array
      // lives on the stack; accounts is a local variable.  So,
      // accounts[count] is on the stack.  However, *(accounts[count])
      // is the thing that is pointed to by accounts[count].  That
      // value is an Acct_C object, and it lives on the heap.`


      // 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;

}







