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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std; // Declare namespace
void output(string, string, double, double, double, double, double, double);
double netPay(double, double, double, double, double);
void main()
{
// Declare variables
string firstName;
string lastName;
double basicRate = 0;
double hoursWorked = 0;
double overtimeRate = 0;
double taxRate = 0;
double otherDeductions = 0;
double net;
int len = 0;
// Declare variables
ifstream infile;
string mystring;
ofstream outfile;
infile.open("EmployeeInfo.txt");
outfile.open("PayStub.txt");
if (!infile)
{
cout << "Cannot open input file. Please check file." << endl;
}
getline(infile, mystring);
len = mystring.find(" ");
firstName = mystring.substr(0, len);
mystring = mystring.erase(0, len + 1);
len = mystring.find(" ");
lastName = mystring.substr(0, len);
mystring = mystring.erase(0, len + 1);
len = mystring.find(" ");
basicRate = atof(mystring.substr(0, len).c_str());
mystring = mystring.erase(0, len +1);
len = mystring.find(" ");
hoursWorked = atof(mystring.substr(0, len).c_str());
mystring = mystring.erase(0, len + 1);
net = netPay(basicRate, hoursWorked, overtimeRate, taxRate, otherDeductions);
output(firstName, lastName, basicRate, hoursWorked, overtimeRate, taxRate, otherDeductions, net);
while (!infile.eof())
{
getline(infile, mystring);
len = mystring.find(" ");
firstName = mystring.substr(0, len);
mystring = mystring.erase(0, len + 1);
len = mystring.find(" ");
lastName = mystring.substr(0, len);
mystring = mystring.erase(0, len + 1);
len = mystring.find(" ");
basicRate = atof(mystring.substr(0, len).c_str());
mystring = mystring.erase(0, len + 1);
len = mystring.find(" ");
hoursWorked = atof(mystring.substr(0, len).c_str());
mystring = mystring.erase(0, len + 1);
len = mystring.find(" " );
overtimeRate = atof(mystring.substr(0, len).c_str());
mystring = mystring.erase(0, len + 1);
len = mystring.find(" ");
taxRate = atof(mystring.substr(0, len).c_str());
mystring = mystring.erase(0, len + 1);
len = mystring.find(" ");
otherDeductions = atof(mystring.substr(0, len).c_str());
mystring = mystring.erase(0, len + 1);
net = netPay(basicRate, hoursWorked, overtimeRate, taxRate, otherDeductions);
output(firstName, lastName, basicRate, hoursWorked, overtimeRate, taxRate, otherDeductions, net);
double basicRate = 0;
double hoursWorked = 0;
double overtimeRate = 0;
double taxRate = 0;
double otherDeductions = 0;
}
infile.close();
outfile.close();
}
double netPay(double basicRate, double hoursWorked, double overtimeRate, double taxRate, double otherDeductions)
{
double gross;
if (hoursWorked <= 40)
{
gross = basicRate * hoursWorked;
double net = gross - (gross * taxRate) - otherDeductions;
return net;
}
else
{
gross = (basicRate * 40) + ((hoursWorked - 40) * (overtimeRate * basicRate));
double net = gross - (gross * taxRate) - otherDeductions;
return net;
}
}
void output(string firstName, string lastName, double basicRate, double hoursWorked, double overtimeRate,
double taxRate, double otherDeductions, double net)
{
cout << fixed << showpoint << setprecision(2);
cout << "___________________________" << endl;
cout << "|" << setw(12) << left << " First Name" << "| " << setw(11) << firstName << "|" << endl;
cout << "___________________________" << endl;
cout << "|" << setw(12) << left << " Last Name" << "| " << setw(11) << left << lastName << "| " << endl;
cout << "__________________________________________________________________" << endl;
cout << "|" << setw(12) << left << " Basic Rate" << "|" << setw(12) << " Hours" << "|" << setw(12) << " O/T Rate" <<
"|" << setw(12) << " Tax Rate" << "|" << setw(12) << " Other" << "|" << endl;
cout << "__________________________________________________________________" << endl;
cout << "| " <<setw(11) << left << basicRate << "| " << setw(10) << hoursWorked << " " << "| " << setw(11) << overtimeRate <<
"| " << setw(11) << taxRate << "| " << setw(11) << otherDeductions << "|" << endl;
cout << "__________________________________________________________________" << endl;
cout << "| " <<setw(50) << left << "Net Pay:" << "| " << setw(11) << net << "|" << endl;
cout << "__________________________________________________________________" << endl << endl << endl;
}
|