Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TEXT FILE emp-data.txt 101 41 8.11 Y 49 666 1 66.66 Y 66 722 32 7.22 N 40 9876 2

ID: 3770924 • Letter: T

Question

TEXT FILE emp-data.txt

101 41 8.11 Y 49
666 1 66.66 Y 66
722 32 7.22 N 40
9876 23 9.99 N 39

CS121 –Lab11
Problem statement:
Create a program that will read an employee file, as below,                                                       
Employee num   Department   PayRate   Exempt   Hours worked
101   41   8.11   Y   49
722   32   7.22   N   40
9876   23   9.99   N   39
666   1   66.66   Y   66
create a output payroll register file, and then search the newly created file(using binary   
search algorithm)
Code:
The output file, or payroll register file, ( user chooses name ) is to have the following data  
   ( each employee on one line )                                       a. Employee number ( left justified )
       b. Department
       c. Pay Rate                                           d. Exempt                                           e. Hours Worked                                       f. Base Pay ( pay rate * hours worked )                           g. Overtime pay                                       h. Total pay                                                                              
Overtime pay is calculated only for nonexempt employees.
An employee is exempt if a “Y” appears in    the exempt column. Overtime is paid for all hours worked over 40. Overtime multiplier is 1.5. If an exempt employee works over 40 hours, the employee is only paid for 40 hours of work.  
Now ask the user if they wish to search the payroll register file.
   If yes, load the employee records from the register file into an array of struct   Assume there are at most fifty records.
   The user will be prompted for an employee number,
Do a binary search on the array of employee structs, determining if the employee   
is in the database.
If employee is in the database, print employee info,
If not, state so with an informational message. Repeat as long as the user wishes.
   The struct is to be named employRec and is to have at least 7 fields
   The array is to be named empArray
   The struct is to be declared in a file called employee.h along with function
prototypes
   The remaining functions for input/output file are to be in the files_ops.h and
files_ops.cpp. Function prototypes should be in the respective header file.
   Function main() is to be in a separate file called main.cpp
Here's a sample search
Please enter an employee number : 666
The record of employee with id 666 is
Department: xyz
PayRate   $xx.xx
Exempt: Y
Hours Worked : xxx
   Base Pay :$xxxx.xx
   Overtime : $xxxx.xx
   Total Pay: $xxx.xx

   Do you wish to search database?
Please enter an employee number : 129
   Employee seems not to exist

You are to create a project/file for this lab named Lab11for the project name and lab11_driver.cpp for the driver.
A sample text file ( “emp_data.txt” ) and binary search program is provided. Check email attachment to see the file your program will read. Download and copy the sample text file in your hard drive.
Open the file in a function – error check

Explanation / Answer

//employee.h
#ifndef employee_h
//employee.h
#define employee_h

struct employRec
{
   int Employee_num;
int Department;   
   double PayRate;
   char Exempt;
   int Hours_worked;
   double Base_Pay;
   double Overtime_Pay;
   double Total_Pay;
};
employRec empArray[50];
double calcBase_Pay(double, int);
double calcOverTime_Pay(char, int);
employRec* binarySearch(int);
#endif

//main.cpp
#include <iostream>
using namespace std;
int main()
{
   int emp_num;
   char ch='y';
   while(1)
   {
   cout<<" Do you wish to search database / payroll register file? <y/n>";
cin>>ch;
if(ch=='y'||ch=='Y')
{
file_read("emp_data.txt");
   cout<<"Please enter an employee number :";
   cin>>emp_num;
   employRec *e=binarySearch(emp_num);
   if(e!=NULL)
   {
   cout<<"The record of employee with id "<<e->emp_num<<" is ";
cout<<"Department: "<<e->Department;
cout<<"PayRate: "<<e->PayRate;
cout<<"Exempt: "<<e->Exempt;
cout<<"Hours Worked: "<<e->Hours_worked;
cout<<"Base Pay: "<<e->Base_Pay;
cout<<"Overtime: "<<
cout<<"Total Pay: "<<e->Total_Pay;  
   }
else
   cout<<" Employee seems not to exist";  
   }
   else
       exit(0);
   }
   return 0;
}

//lab11_driver.cpp
#include "main.cpp"
main();

//files_ops.h
#define files_ops_h
   void file_read(string filename);
   void file_write(string filename);
#endif

//files_op.cpp
#include <fstream>
int file_read(string filename)
{
   int length=0;
   ifstream infile(new File(filename));
   if(infile==NULL)
   {
       cout<<"input file not available";
       exit(0);
   }
   else
   {
       while(!infile.eof())
       {
           infile>>empArray[length].Employee_num;
           infile>>empArray[length].Department;
           infile>>empArray[length].Exempt;
           infile>>empArray[length].PayRate;
           infile>>empArray[length].Hours_worked;
           empArray[length].Base_Pay=calcBase_Pay(empArray[length].PayRate,empArray[length].Hours_worked);
           empArray[length].Overtime_Pay=calcOverTime_Pay(empArray[length].Exempt,empArray[length].Hours_worked);
           empArray[length].Total_Pay=   empArray[length].Base_Pay+empArray[length].Overtime_Pay;
           length++;
       }
   }
   return length;
}
void file_write(string filename,int length)
{
   ofstream outfile(new File(filename));
   if(outfile==NULL)
   {
       cout<<"Output file not available";
       exit(0);
   }
   else
   {
       for(int i=0;i<length;i++)
       {
           outfile<<empArray[i].Employee_num<<""<<empArray[i].Department;
           outfile<<empArray[i].Exempt;
           outfile<<empArray[i].PayRate;
       }
   }
}

//employee.cpp
#inlcude "employee.h"
double calcBase_Pay(double payRate , int hoursWorked)
{
   return payRate*hoursWorked;
}

double calcOverTime_Pay(char excempt, int hoursWorked)
{
   double overtime=0;
//Overtime pay is calculated only for nonexempt employees.
//An employee is exempt if a “Y” appears in the exempt column.
if(excempt!='Y')
{
   if(hoursWorked>40)
   {
       /*Overtime is paid for all hours worked over 40. Overtime multiplier is 1.5.*/
       overtime=(hoursWorked-40)*1.5;
   }
}   
return overtime;
}

employRec* binarySearch(int empno, int length)
{
   employRec* emp=NULL;
   for(int i=0;i<length;i++)
   {
       if(empArray[i].Employee_num==empno)
       {
           emp=new employRec;
           //emp.Department=empArray[i].Department;
           emp.Exempt=empArray[i].Exempt;
           emp.PayRate=empArray[i].PayRate;
           emp.empArray[length].Hours_worked;
           empArray[length].Base_Pay=calcBase_Pay(empArray[length].PayRate,empArray[length].Hours_worked);
           empArray[length].Overtime_Pay=calcOverTime_Pay(empArray[length].Exempt,empArray[length].Hours_worked);
           empArray[length].Total_Pay=   empArray[length].Base_Pay+empArray[length].Overtime_Pay;
       }
   }
  
  
}