I realize that the logic used in the Payroll program Part 2 may be unfamiliar to you, so the following code is offered as a sample, from the main module. The main program lets the user enter a period ending date. It searches for the date in the Payroll file. For each matching Payroll record, the program locates a matching record in the Employee file (using the Employee ID number). Payroll data for each employee are calculated and displayed. Four-digit years are used on all dates. Each Payroll object contains a Date object, defined by the Date class--see the hypertext link from the October lecture notes page.
#include <fstream.h> #include <conio.h> #include <stdlib.h> #include "employee.h" #include "payroll.h"
/* Search for the first record in the Employee file having a matching Employee ID. If found, return the Employee object initialized with data from the file, and return boolean True. */ bool LookForEmployee( Employee & E, ifstream & efile ) { Employee temp(E); // make a copy while( !efile.eof() ) { efile >> E; if( temp.GetId() == E.GetId() ) return true; } return false; } void wait() { cerr << "\nPress any key to continue..."; getch(); cerr << endl; } /* Given a date to find, search for all records in the Payroll file having the same date. When each is found, use its employee ID to find a matching Employee record. Return true if the date was found, false otherwise. */ void LookForDate( ifstream & efile, ifstream & pfile ) { bool wasFound = false; long dateToFind; cout << "Enter a Period Ending Date (yyyymmdd format): "; cin >> dateToFind; cout << '\n'; while( !pfile.eof()) { Payroll P; pfile >> P; if( P.GetEndingDate().EqualTo(dateToFind) ) { Employee E( P.GetEmpId() ); if( LookForEmployee( E, efile )) { E.SetPayroll( P ); // store Payroll object inside Employee E.ShowReport( cout ); wait(); } wasFound = true; } } if( !wasFound ) cout << "That period ending date was not found." << endl; } // Open both input file streams, return false if either // cannot be opened. bool OpenInputFiles( ifstream & efile, ifstream & pfile ) { efile.open( "employee.txt", ios::in | ios::nocreate ); if( !efile ) { cerr << "Employee file not found; cannot continue.\n"; return false; } pfile.open("payroll.txt", ios::in | ios::nocreate ); if( !pfile ) { cerr << "Payroll file not found; cannot continue.\n"; return false; } return true; } int main() { cout.setf( ios::fixed | ios::showpoint ); cout << setprecision(2); cout << "Employee Payroll Program (Exercise 4.8.2, Part 2)" << endl; ifstream efile; ifstream pfile; if( !OpenInputFiles( efile, pfile )) return -1; LookForDate( efile, pfile ); return 0; }