// student.cpp -- Implementation of the student structure

#include <iomanip>

#include "student.h"


// Constructor for Student: just default values
Student::Student()
{
  lname = "";
  fname = "";
  mid = final = hmwks = 0;
}

// Constructor for Student: specific initialization for the fields
Student::Student(char flname[], char ffname[], int fmid, int ffinal, int fhmwks)
{
  mid = fmid;
  final = ffinal;
  hmwks = fhmwks;
  lname = flname;
  fname = ffname;
}

// Compute grade of a student
double Student::grade()
{
  return (0.4*final+0.4*hmwks+0.2*mid);
}


// The functions below are help functions for Student.
// They are neither member functions nor friends of the class. Thus
// they have no access to the private members of the Student class.

// Reads a file filein of students into array a with at most n records
// and returns the number of records actually read (-1 if there is no
// such file)
int readStudentFile(const char *filein, Student a[], int n)
{
  int count = 0;
  ifstream fin(filein);
  
  if (fin.fail())
    return -1;
  
  while (!fin.eof())
    {   
      fin >> a[count] >> ws;
      count++;
    }
  fin.close();
  return count;
}

// Writes a file fileout of students record from the first n
// elements of the array a of Students. It returns 0 in case of success,
// -1 if failure
int writeStudentFile(const char *fileout, const Student a[], int n)
{
  ofstream fout(fileout);
  if (fout.fail())
    {
      return -1;
    }
  for (int row = 0; row < n; row++)
    {
      fout << a[row] << '\n';
    }
  fout.close();
  return 0;
}


// It prints out to the standard output the first
// n records of a
void printStudentTable(const Student a[], int n)
{
  cout << "LAST NAME       FIRST NAME           MIDTERM   FINAL   HOMEWORKS" << endl 
       << "====================================================================" << endl;
  for (int row=0; row < n; row++)
    {
      cout << a[row] << endl;
    }
}






