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

CS121 –Lab11 Problem statement: Create a program that will read an employee file

ID: 3770911 • Letter: C

Question

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

THIS IS IN C++

Explanation / Answer

BELOW ARE THE CODE HAVING COMMENTING EACH LINE WITH EXPLAINATION.

#include <iostream>

#include <cstdio>

#include <fstream>

#include <string>

#include <iomanip>

#include <cstring>

using namespace std;

const int SIZ = 20;          // size of arrays to hold employee

struct payroll       // strucute of employees

{

      double workedhr;   // employess hours worked

      double payrate;          // employees rate of pay

      string empNum;         // employee ID

      string fname;     // first name of employee

      string lname;      // last name of employee

      double regularpay;    // employee regular pay  

      double overtimepay;   // employee overtime pay

      double overtimehours; // employee overtime hours

      double grosspay;      // employee gross pay

      int regularhours;     // employee regular hours

      int Sort;       // sort employees through last name

      int swap;             // swaps employees last name

};

void User();       // tells the user about the employee

int readInput(payroll[]); // reads data from a txt file

void Sort(payroll[], int); // sorts the through employees last name

void swap(payroll*, payroll*); // swaps the lastname throught alphabertical order

void regularhours(payroll[], int); // calculates if the employee has exceeded it regular hours or not

void regularpay(payroll[], int);   // calculates the employees regular pay

void overtimehours(payroll[], int); // calculates if the employee has exceeded it overtime hours or not

void overtimepay(payroll[], int); // calculates the employees overtime pay

void grosspay(payroll[], int); // calculates the employees gross pay

int Report(payroll[], int); // writes/shows employees payroll information

int main()

{

    int i, C, R;        // counter

    payrollemployees[SIZ]; // array

    int numemp;             // total number of .

    User();              // tell user what program does

    numemp= readInput(employees);

    cout << "There were " << numemp << " . ";   // how many employees are there

    if (numemp < 1)

    {

       cout << "Program ending ";

       C = 1;

    }

    else

    {

       // sort employees

       cout << "Sorting the employees in order with the lastname first ";

       Sort(employees, numemp); // sort employees lastname

       findregularhours(employees, numemp); // calculates if the employee has exceeded it regular hours or not

       regularpay(employees, numemp); // calculates the employees regular pay

       overtimehours(employees, numemp); // calculates if the employee has exceeded it overtime hours or not

       overtimepay(employees, numemp); // calculates the employees overtime pay

       grosspay(employees, numemp); // calculates the employees gross pay

       C = Report(employees , numemp); // writes/shows employees payroll information on screen and prints to outputFile

    }

   Return C;

}

/* function User */

void User()

{

    cout << " This program reads a file called employees.txt, ";

    cout << "and it calculates the regular, overtime and gross pay for each golfer. ";

    cout << "employees are sorted into order with employees last name. ";

    cout << "output is written to the screen. and to an output file ";

    cout << "It uses an array of structures to store the data ";

}

/* readInput */

int readInput(payrollemp[])

{

   ifstream inputFile; // input filename in program

   int numEmp; // number employees

   int i;// counter

   // open file and read input from file employees2.txt

   inputFile.open("employees2.txt");

   if (inputFile.fail())

   {

      cout << "Error opening file employees2.txt ";   //error opening txt file

      cout << "end of program ";

     Return 0;

   }

   else

   {

      i = 0; // employee number for array

      while ( ( i < SIZ) && (inputFile >> emp[i].workedhr))

      { // there is a line of data to read and array has room

         inputFile >> emp[i].payrate;

         inputFile >> emp[i].empNum;

         inputFile >> emp[i].fname;

         inputFile >> emp[i].lname;

         i++; // ready for the next employee

      } // end while

   numEmp = i; // number employee

   inputFile.close(); // close the file after reading it

   cout << "input file closed ";

Return numEmp; // Return number employee read in file

   }

}

/* Function swap*/

void swap(payroll&empa, payroll&empb)

{

    payrolltemp; // place to store one employee data

    temp = empb;

    empb = empa;

    empa = temp;

}

/* sort with flag */

void Sort(payrollemp[], int numE)

{

    bool swapmade = false; // when true a swap was made in this pass

    int i, lastpos; // last position to look for correct order

    // sort the rmployees lname

    cout << "sorting employees with their last name ";

    lastpos = numE;

    do

    {

      lastpos--;

      swapmade = false;

      for ( i = 0; i < lastpos; i++)

      {

          if (emp[i].lname > emp[i+1].lname) // they are in the wrong order

          { // swap all items here

                 swap(emp[i], emp[i+1]);

                 swapmade = true;

          }

          else

          {   

             if (emp[i].lname == emp[i+1].lname) // they are in the wrong order

                if (emp[i].fname == emp[i+1].fname) // same employees with lname

                { // swap all items here

                   swap(emp[i], emp[i+1]);

                   swapmade = true;

                }

          }

      }

   } while(swapmade);

}

/* regularhours ()*/

void regularhours(payrollemp[], int n) // find regular hours

{

   for(int i=0; i<n; i++)

   {

       if(emp[i].workedhr > 40)     //if the hours is > 40 it will make it 40

           emp[i].regularhours=40; //regular hour = 40

       else

           emp[i].regularhours=emp[i].workedhr;

   }

}

/*regularpay ()*/

void regularpay(payrollemp[], int n)   // find regular pay

{

   for(int i=0; i<n; i++)

   {

      emp[i].regularpay = emp[i].regularhours*emp[i].payrate;

   }

}  

/*overtime()*/

void overtimehours(payrollemp[], int n) // find overtime hours

{

   for(int i=0; i<n; i++)

{

       if(emp[i].workedhr>40)

            emp[i].overtimehours=emp[i].workedhr-40; //calculates and finds how many hours are overtime

       else

           emp[i].overtimehours=0; //if there is no overtime

   }

}

/*overtimepay()*/

void overtimepay(payrollemp[], int n) // find overtime pay

{

    for(int i=0; i<n; i++)

    {

        emp[i].overtimepay=emp[i].overtimehours*emp[i].payrate*1.5;   // calculating overtimepay

    }

}

/*grossPay()*/

void grosspay(payrollemp[], int n) // find the gross pay of each employee

{

   for(int i=0; i<n; i++)

   {

       emp[i].grosspay=emp[i].regularpay+emp[i].overtimepay; // calculate gross pay

   }

}

/*output report*/

int Report(payrollemp[],int numE) //writes report and shows payroll information

{

    int i; // counter

    double totalgrosspay = 0.0;

   // emp[i].totalgrosspay += emp[i].grosspay;

    ofstream outputFile; // output file

    // open report file and write output data ********************

    cout << "Report being written to file payrollreport.txt ";

    outputFile.open("payrollreport.txt"); // output file

    if (outputFile.fail()) // if the payroll information was not shown on screen

    {

        cout << "output file did NOT open ";

        cout << "output will only be sent to the screen ";

        cout <<"First          Last       Employee     Hours        Rate      Regular    Overtime    Gross "; //table of employees payroll data sheet to the screen

        cout <<"Name           Name        Number      Worked      of Pay       Pay        Pay        Pay ";

        cout <<"=========================================================================================== ";

        for ( i=0; i < numE; i++)

        {

            cout << emp[i].fname << " ";   // employees first name

            cout << emp[i].lname << " ";    // employees last name

            cout << emp[i].empNum << " ";       // employee ID

                   cout << emp[i].workedhr << " "; // hours worked

            cout << emp[i].payrate << " "; // hourly rate

                   cout << emp[i].regularpay << " "; // regualr pay

            cout << emp[i].overtimepay << " "; // overtime pay

                   cout << emp[i].grosspay << " ";    // gross pay

            totalgrosspay += ((int)((emp[i].grosspay + 0.005) * 100)) / 100.0; // total gross pay

       }

    }

    else

    {

         cout <<"First          Last       Employee     Hours        Rate      Regular    Overtime    Gross "; //table of employees payroll data sheet to the screen

         cout <<"Name           Name        Number      Worked      of Pay       Pay        Pay        Pay ";

         cout <<"============================================================================================= ";

         outputFile <<"First          Last        Employee     Hours       Rate      Regular    Overtime    Gross ";//table of employees payroll data sheet to txt file

         outputFile <<"Name           Name         Number      Worked     of Pay      Pay        Pay        Pay ";

         outputFile <<"=========================================================================================== ";

        // loop to print report of employees ********************

        for ( i=0; i < numE; i++)

        {

             // writes employee payroll data to payrollreport.txt

                        outputFile << setw(13) << fixed << left << emp[i].lname << " ";

             outputFile << setw(12) << emp[i].fname << " ";

             outputFile << setw(11) << emp[i].empNum << " ";

             outputFile << setw(9) << fixed << setprecision(2) << emp[i].workedhr<< " ";

             outputFile << setw(8) << right << fixed << setprecision(2) << emp[i].payrate << " ";

             outputFile << setw(10) << fixed << setprecision(2) << emp[i].regularpay << " ";

             outputFile << setw(10) << fixed << setprecision(2) << emp[i].overtimepay << " ";

             outputFile << setw(10) << right << fixed << setprecision(2) << emp[i].grosspay << " ";

             // shows employee payroll data on screen

             cout << setw(14) << fixed << left << emp[i].lname << " ";

             cout << setw(12) << emp[i].fname << " ";

             cout << setw(11) << emp[i].empNum << " ";

             cout << setw(8) << fixed << setprecision(2) << emp[i].workedhr<< " ";

             cout << setw(8) << right << fixed << setprecision(2) << emp[i].payrate << " ";

             cout << setw(10) << fixed << setprecision(2) << emp[i].regularpay << " ";

             cout << setw(10) << fixed << setprecision(2) << emp[i].overtimepay << " ";

             cout << setw(10) << right << fixed << setprecision(2) << emp[i].grosspay << " ";

            

               }

        cout <<"============================================================================================ ";

        cout <<" Total Gross Pay        $ " << fixed << setprecision(2) << totalgrosspay << " ";

        outputFile <<"============================================================================================ ";

        outputFile <<" Total Gross Pay          $ " << fixed << setprecision(2) << totalgrosspay << " ";

               outputFile.close();

       cout << "Output file closed ";

    } // end else output file open

   Return 0;

}