// readNames3.cc read first and last names from a file
// using getline
// P. Conrad for CISC181 06S   02/17/06

#include <iostream>
#include <fstream>

#include <cstring> // equivalent to doing #include <string.h> in C

using namespace std;

// following line of code changes every instance of NAMELEN
// to be 32

// the terminology for such a line is
//    #define constant
//    more generally, anything that starts with # is a pre-processor directive

// 

#define NAMELEN 32

#define INPUT_FILENAME "fullNames.dat"

int main(void)
{

  // open the file for our first pass.. which is to
  // find the maximum length

  ifstream infile(INPUT_FILENAME, ios::in);

  if (!infile)
    {
      cerr << "Could not open "<< INPUT_FILENAME << endl; // !??!?
      exit(1);
    }

  char name[NAMELEN];


  // unlike infile >> name, which will use whitespace to separate
  // one string from the next, infile.getline() will read an entire
  // line from our file

  int maximumLength = 0;

  infile.getline(name,NAMELEN);  // we changed this line
  
  while (!infile.eof())
    {
      // process the input line

      int length = strlen(name);

      if (length > maximumLength       )   // 5 pts Extra Credit bbendigo 
	{
	  maximumLength = length;

	}


      // try to read the next line
      
      infile.getline(name, NAMELEN);  // and this one
    }

  infile.close(); // close the file when we are done with it

  cout << "The maximum length was " << maximumLength;


  // open the file for our second pass;
  // print all names of maximum length

  ifstream infile2(INPUT_FILENAME, ios::in);

  if (!infile2)
    {
      cerr << "Could not open "<< INPUT_FILENAME << endl; // !??!?
      exit(1);
    }

  // unlike infile2 >> name, which will use whitespace to separate
  // one string from the next, infile2.getline() will read an entire
  // line from our file

  infile2.getline(name,NAMELEN);  // we changed this line
  
  while (!infile2.eof())
    {
      // process the input line

      int length = strlen(name);

      if (length == maximumLength  )  
	{
	  cout << name << " is of maximum length" << endl;
	}


      // try to read the next line
      
      infile2.getline(name, NAMELEN);  // and this one
    }

  infile2.close(); // close the file when we are done with it



  return 0; // successful completion.

}

