// readNames4.cc read names from a file with both first and last names
// print the length of every name

// Use a "two pass process" to
//   first find how long is the longest name
//   second, print all names of that length

// P. Conrad for CISC181 06S   02/17/06

#include <iostream>
#include <fstream>

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)
{
  
  ifstream infile(INPUT_FILENAME, ios::in);

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

  char name[NAMELEN];

  // where infile >> name; reads character until it hits white space,
  // and skips over white space, infile.getline(name,NAMELEN) reads an
  // entire line, until a \n  (and it skips over the \n)

  int lengthOfLongest = 0;

  infile.getline(name, NAMELEN);
  
  while (!infile.eof() )
    {

      // process the name

      int length = strlen(name);


      if ( length > lengthOfLongest)  // 5 Extra Credit points timstrab
	{

	  lengthOfLongest = length;

	}

      // try to read the next name

      infile.getline(name, NAMELEN);
    }

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

  cout << "length of longest name is " << lengthOfLongest << endl;

  ifstream infile2(INPUT_FILENAME, ios::in);

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

  // where infile2 >> name; reads character until it hits white space,
  // and skips over white space, infile2.getline(name,NAMELEN) reads an
  // entire line, until a \n  (and it skips over the \n)


  infile2.getline(name, NAMELEN);
  
  while (!infile2.eof() )
    {

      // process the name

      int length = strlen(name);
      if ( length==lengthOfLongest)  // 5 Extra Credit points timstrab
	{
	  cout << name << " is a winner for longest name " << endl;
	}

      // try to read the next name

      infile2.getline(name, NAMELEN);
    }

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

  return 0; // successful completion.

}





