/* This program counts the lines in a file named "Document".

   Input(Document): A sequence of characters
   Output(Screen):  The number of lines in Document
------------------------------------------------------------------*/

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

unsigned LineCount(const char[]);

int main(void)
{
   const char
      InputFileName[] = "Document";

   unsigned
      NumberOfLines = LineCount(InputFileName);

   cout << "\nThe file named '" << InputFileName
        << "' contains " << NumberOfLines << " lines.\n\n";

   return 0;
}


/*------------------------------------------------------------------
LineCount counts the number of lines in a file named FileName.

   Receive:  FileName, the name of the file whose lines are to 
             be counted
   Return:   LCount, the number of lines in the file
-------------------------------------------------------------------*/

unsigned LineCount(const char FileName[])
{
   const char
      OpenFailedMsg[] = "\n*** LineCount: unable to open file: ";

   fstream                // open stream to FileName
      InStream(FileName, ios::in);

   if (InStream.fail())
   {
      cerr << OpenFailedMsg << FileName << endl;
      exit (-1);
   }

   unsigned
      LCount = 0;         // counter variable
   char
      Ch;                 // input character container

   InStream.get(Ch);      // get first character
   while (!InStream.eof())// while more data:
   {
      if (Ch == '\n')     //   if Ch is a newline
         LCount++;        //     increment the counter
      InStream.get(Ch);   //   get next character
   }                      // end while

   InStream.close();

   return LCount;
}
