// readFootballSched.cpp
// P.Conrad, Spring 2004
// CISC181, A program to read a football schedule from a file,
// and compute the win/loss/tie record.


// Assumptions:
//   It is assumed that the file "udel.txt"
//   is in the "current directory" at the time the program is invoked.
//   The output is written to the screen.

#include <iostream>

using std::cout;
using std::cin;
using std::flush;

#include <fstream> // for reading directly from a disk file
using std::ifstream; // input file stream


int main(void)
{

  // try to open an input file

  ifstream gameFile("udel.txt"); // open the file

  // test whether the file was opened correctly using the
  // overloaded ! operator; returns false if opening the file failed

  if (!gameFile) 
    {
      cerr << "udel.txt file could not be opened" << endl;
      exit (-1);
    }
    

  const int DATE_LENGTH = 8; // mm/dd/yy format

  char date[DATE_LENGTH+1];  // +1 because of \0

  const int MAX_DOMAIN_LENGTH = 30 ; // domain name of college or university

  char homeTeamDomain[MAX_DOMAIN_LENGTH+1];  // +1 because of \0
  char opponentDomain[MAX_DOMAIN_LENGTH+1];  // +1 because of \0

  char homeOrAway; // single char, H or A
  
  int homeScore, opponentScore;

  int wins=0, losses=0, ties=0; // initialize variables

  // read hometeam's domain name from input file
  

  gameFile >> homeTeamDomain; // note: gameFile in place of cin

  // read date of first game

  gameFile >> date; // read in the date.  We have to read it, even if we don't
                    // use it in this program.

  while (!gameFile.eof())  // will stop at end of file, or at CTRL/D
    {

      // read in the rest of the data

      gameFile >> homeOrAway;
      gameFile >> opponentDomain;
      gameFile >> homeScore;
      gameFile >> opponentScore;

      // determine if won, lost, or tied

      if (homeScore > opponentScore)
	wins ++;
      else if (homeScore < opponentScore)
	losses ++;
      else
	ties ++;
      
      // output a report of the game

      cout << "On " << date << ", " << homeTeamDomain;
      
      if (homeScore > opponentScore)
	cout << " beat ";
      else if (homeScore < opponentScore)
	cout << " lost to ";
      else
	cout << " tied with ";

      cout << opponentDomain << "." << endl;

      
      // get the date of the next game

      gameFile >> date;
      
    }

  // output summary

  cout << "win: " << wins << " lose: " << losses << " tie: " << ties << endl;

  return 0;
} 


