1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
//header files
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
//function prototypes
void instructions();
void reportTitle();
void displayEmployeeInfo(string& employeeName, double hourlyRate, double hoursWorked, double taxRate, double grossAmount, double netAmount);
void totalAmounts(double totalGrossAmount, double totalNetAmount);
void readData(istream& inFile, string& employeeName, double& hourlyRate, double& hoursWorked, double& taxRate);
void calculate(double hourlyRate, double hoursWorked, double taxRate, double& grossAmount, double& netAmount, double& totalGrossAmount, double& totalNetAmount);
int main()
{
//declare input file variable
ifstream inFile;
// declare input file variables
string employeeName;
double hourlyRate, hoursWorked, taxRate, grossAmount = 0, netAmount = 0, totalGrossAmount = 0, totalNetAmount = 0;
// open the input file.
inFile.open("payroll.txt");
/*check for input failure in case the drive is not accessible. If true, display a message that the input file is not accessible, return the integer 1 from the main program.*/
if (!inFile)
{
cout << "input failed." << endl;
return 1;
}
//call functions for program instructions, formatting, and info display in columns
instructions();
reportTitle();
cout << endl;
while (!inFile.eof())
{
readData(inFile, employeeName, hourlyRate, hoursWorked, taxRate);
calculate(hourlyRate, hoursWorked, taxRate, grossAmount, netAmount, totalGrossAmount, totalNetAmount);
displayEmployeeInfo(employeeName, hourlyRate, hoursWorked, taxRate, grossAmount, netAmount);
}
inFile.close();
cout << endl;
cout << endl;
totalAmounts(totalGrossAmount, totalNetAmount);
cout << endl;
return 0;
}
//displays program instructions
void instructions()
{
cout << "This payroll program calculates an individual employee pay and company totals\nusing data from the data file payroll.txt.\n\n\nA payroll report showing payroll information is displayed.\n" << endl;
}
/*function reportTitle displays the payroll report titles in columnar format*/
void reportTitle()
{
//set program formatting
cout << setprecision(2) << fixed << showpoint << left;
//display report titles
cout << setw(5) << "Emp." << setw(15) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours" << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
cout << setw(5) << "No. " << setw(15) << "Name" << setw(10) << "Rate" << setw(10) << "Worked" << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
}
/*function displayEmployeeInfo displays employee info in while loop line by line until end of file is reached*/
void displayEmployeeInfo(string& employeeName, double hourlyRate, double hoursWorked, double taxRate, double grossAmount, double netAmount)
{
static int counter = 1;
cout << left << setw(5) << counter << setw(15) << employeeName << right << setw(5) << hourlyRate << setw(10) << hoursWorked << setw(10) << taxRate << setw(10) << grossAmount << setw(10) << netAmount << endl;
counter = counter +1;
}
/*function totalAmounts displays total gross and net amount*/
void totalAmounts(double totalGrossAmount, double totalNetAmount)
{
cout << left << setw(20) << "Totals:" << right << setw(40) << totalGrossAmount << setw(10) << totalNetAmount;
}
// function readData reads input data from payroll.txt
void readData(istream& inFile, string& employeeName, double& hourlyRate, double& hoursWorked, double& taxRate)
{
getline(inFile,employeeName,'#');
inFile >> hourlyRate >> hoursWorked >> taxRate;
}
// function calculate calculates gross amount and net amount and total gross and net amount from all employees.
void calculate(double hourlyRate, double hoursWorked, double taxRate, double& grossAmount, double& netAmount, double& totalGrossAmount, double& totalNetAmount)
{
grossAmount = hourlyRate*hoursWorked;
netAmount = grossAmount - grossAmount * (taxRate/100);
totalGrossAmount = totalGrossAmount + grossAmount;
totalNetAmount = totalNetAmount + netAmount;
}
|