Home‎ > ‎Assignments‎ > ‎Hour of Code‎ > ‎Solutions‎ > ‎

Payroll I Solution

Employee Payroll Report I - Solution

//employee.cpp, run under Borland C++ 5.02
//Programming Assignment #10 - Employee Payroll Report
//Target Type: win32 application; Target Model: console
//Author:  Linda Weiser Friedman
//Date compiled: November 14, 1999.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

                                                                //function prototypes
void PrintSummaryInfo(int, int, int, double, double);
void PrintReportHeadings(string);
void PrintColumnHeadings();
void PrintDetailLine(string, float, int, int, double, double);
void PrintaLineOf(int, char);
void SkipLines (int);
void indent (int);
void PrintPersonalInfo();
void DisplayUserInfo();
int EnterEmployeeData(string&, int&, float&);

ofstream outReport ("f:\\reports\\payroll.txt"); //output text file - Report

int main (){
    string name, period;                //variable declarations
   int n=0, hours, hoursTot=0, regHours, ovtHours, ovtHoursTot = 0;
    float payRate;
    double ovtSal, totSal, ovtSalTot=0, payrollTot= 0;

    if (!outReport) cerr << "Error: could not open output file\n";
   else {        //file OK - do rest of main
                        
    DisplayUserInfo();
   cout << "\n\nWhat pay period will this report cover? (no spaces please)  ";
   cin >> period;
   PrintPersonalInfo();
    PrintReportHeadings(period);

   while (EnterEmployeeData(name, hours, payRate)){
      if (hours > 40) {regHours = 40; ovtHours = hours - 40;}
          else {regHours = hours; ovtHours = 0;}

        ovtSal = (ovtHours <= 10) ?
          ovtHours*1.5*payRate :
            (1.5*payRate*10 + 2*payRate*(ovtHours-10));

      totSal = regHours * payRate + ovtSal;
        PrintDetailLine(name, payRate, hours, ovtHours, ovtSal, totSal);
        ovtHoursTot  += ovtHours;
      hoursTot  += hours;
      ovtSalTot += ovtSal;
      payrollTot += totSal;
      n++;
        } //end while

    PrintSummaryInfo(n, hoursTot, ovtHoursTot, ovtSalTot, payrollTot);
    cout << "\n\n\nClose console window?  ";  char c; cin>>c;
   }//end if/else from testing files
   return 0;
} //end main


int EnterEmployeeData(string &name, int &hours, float &payRate){
    char ch = 'y'; string lname, fname;
    cout << "\nEntering employee data? (y or n)";
    cin >> ch;
    if ((ch == 'n') || (ch == 'N')) return 0;
        else {
          cout << "Enter employee's Last Name:  "; cin >> lname;
          cout << "Enter employee's First Name:  "; cin >>fname;
          name = fname + ' ' + lname;
          cout << "Enter employee's hourly pay rate:  "; cin >> payRate;
          cout << "Enter employee's hours worked;  "; cin >> hours;
            return 1;
            }//end else
}//end function EmployeeData

void DisplayUserInfo(){
    cout << "\nThis program will allow you to enter employee payroll data for"
       << "\na particular pay period, one employee at a time.  When all data entry"
      << "\nis finished, it will compute employee salaries and print a report"
      << "\nincluding summary data for your department."
      << "\n\nReport may be found in:  f:\\reports\\payroll.txt" << endl;
}
                                                        //functions for printing the report
void PrintPersonalInfo() {
    PrintaLineOf (65, ':');
    SkipLines(1);
    outReport << "Name:  Linda Weiser Friedman\n"
        << "Course:  CIS 3100\n"
        << "Section: TR6\n";
    SkipLines(1);
}

void PrintReportHeadings(string period){
    PrintaLineOf (65, ':');
   indent(15);
    outReport << "School of Business Employee Roster\n";
    PrintaLineOf (65, ':');
   SkipLines(1);
   indent (20);
   outReport << "Employee Payroll Report" << endl;
   indent (23);
   outReport << "Period:  " << period <<endl;
   SkipLines(1);
   PrintaLineOf (65, '.');
   SkipLines(1);
    PrintColumnHeadings();
    PrintaLineOf (65, '.');
}

void PrintColumnHeadings(){
    outReport << setiosflags(ios::right)
       << setw (15) << ' ' << setw (10) << "Hourly"
       << setw(10) <<" " << setw(10) << "Overtime"
      << setw(10) << "Overtime" << setw(10) <<"Total" << endl
      << setiosflags(ios::left) << setw (15) << "Name"
      << setiosflags(ios::right)
      << setw (10) << "Rate" << setw(10) << "Hours" << setw(10) << "Hours"
      << setw(10) << "Salary" << setw(10) <<"Salary" << endl;
}

void PrintDetailLine(string name, float rate, int hrs, int ovtHrs,
    double ovtSal, double salTot){
    outReport << setiosflags (ios::fixed) << setprecision(2)
        << setw (15) << setiosflags (ios::left) << name
      << setiosflags(ios::right)
      << setw (10) << rate
        << setw (10)<<  hrs
      << setw (10)<< ovtHrs
        << setw (10)<< ovtSal
      << setw (10) << salTot << endl;
}

void PrintSummaryInfo(int n, int hrsTot, int ovtHrsTot, double ovtSalTot,
double payrollTot)
{
   PrintaLineOf (65, '.');
   outReport << setiosflags(ios::showpoint | ios::fixed | ios::right)
   << setw(35)  << hrsTot
   << setw(10)  << ovtHrsTot
   << setw(10)  << ovtSalTot
   << setw(10)  << payrollTot << endl;
    SkipLines (2);
   outReport << "Number of employees this period:  " << n << endl;
    PrintaLineOf (65, ':');
}

void PrintaLineOf(int n, char c){
for (int i=1; i<=n; i++)
    outReport << c;
outReport << endl;
}

void SkipLines (int n){
for (int i=1; i<=n; i++)
    outReport << endl;
}

void indent (int n){
for (int i=1; i<=n; i++)
    outReport << ' ';
}






 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
               School of Business Employee Roster
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

                    Employee Payroll Report
                       Period:  Fall1999

.................................................................

                   Hourly            Overtime  Overtime     Total
Name                 Rate     Hours     Hours    Salary    Salary
.................................................................
Rude   Guiliani     14.00     40.00      0.00      0.00    560.00
Cosmo  Kramer       15.75     35.00      0.00      0.00    551.25
Marcia Brady        14.00     50.00     10.00    210.00    770.00
Lois   Lane         12.25     43.20      3.20     58.80    548.80
Joey   Stalin       17.50     52.50     12.50    350.00   1050.00
.................................................................
                             220.70     25.70    618.80   3480.05


Number of employees this period:  5
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



Comments