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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void GetData(ifstream &, ofstream &, string &, string &, float &, float &, int &, char &, float &, float &, int &, char &);
float Gross(float wage, float hours);
float FITW(float gross, int exemp, char status);
float FICA(float gross);
void SendData(ofstream &, string, string, float, float, float, float, float, float);
int main()
{
ifstream infile;
ofstream Outfile;
ofstream errorfile;
string firstName, lastName;
float wage, hours, hold, gross, tax, net, wageError, hourError;
int exemp, exempError;
char status, statusError;
cout << "Start of program." << endl;
cout << "Opening infile..." << endl;
infile.open("emp.txt");
if (!infile)
{
cout << "Cannot open file. Terminating program." << endl;
exit(1);
}
else
{
cout << "File opened successfully." << endl;
}
cout << "Opening outfile..." << endl;
Outfile.open("pay.txt");
if (!Outfile)
{
cout << "Cannot open file. Terminating program." << endl;
exit(1);
}
else
{
cout << "File opened successfully." << endl;
}
cout << "Opening errorfile..." << endl;
errorfile.open("error.txt");
if (!errorfile)
{
cout << "Cannot open file. Terminating program." << endl;
exit(1);
}
else
{
cout << "File opened successfully." << endl;
}
while (!infile.eof())
{
GetData(infile, errorfile, firstName, lastName, wage, hours, exemp, status, wageError, hourError, exempError, statusError);
if (!exempError && !statusError && !wageError && hourError)
{
gross = Gross(hours, wage);
cout << gross << endl;
tax = FICA(gross);
cout << tax << endl;
hold = FITW(gross, exemp, status);
cout << hold << endl;
net = gross - tax - hold;
cout << net << endl;
SendData(Outfile, firstName, lastName, hours, wage, gross, net, tax, hold);
}
}
cout << "End" << endl;
infile.close();
errorfile.close();
return 0;
}
/*****************************************************/
void GetData(ifstream & infile, ofstream & errorfile, string & firstName, string & lastName, float & wage, float & hours, int & exemp, char & status, float & wageError, float & hourError, int & exempError, char & statusError)
{
infile >> firstName >> lastName >> wage >> hours >> exemp >> status;
wageError = wage <= 0;
hourError = hours < 0;
exempError = exemp < 0;
statusError = (status < 'M' || status > 'M' && status < 'S' || status > 'S' && status < 'm' || status > 'm' && status < 's' || status > 's');
if (statusError)
{
cout << "Marital status error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
errorfile << "Marital status error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
}
if (wageError)
{
cout << "Wage error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
errorfile << "Wage error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
}
if (hourError)
{
cout << "Hour error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
errorfile << "Hour error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
}
if (exempError)
{
cout << "Exemption error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
errorfile << "Exemption error for: " << firstName << " " << lastName << " " << wage << " " << hours << " " << exemp << " " << status << endl;
}
}
/*****************************************************/
float Gross(float wage, float hours)
{
return (wage*hours);
if (hours > 40)
return (wage * 40 + (hours - 40)*1.5*wage);
}
/*****************************************************/
float FITW(float gross, int exemp, char status)
{
double income;
income = gross - (exemp*73.08);
if ((income <= 721) && (status == 'S' || 's'))
return (0);
else if ((income>721 && income <= 7510) && (status == 'S' || 's'))
return ((income - 721)*0.28 + 93.60);
else
return ((income - 7510)*0.35 + 2167.16);
if ((income <= 1515) && (status == 'M' || 'm'))
return (0);
else if ((income>1515 && income <= 7624) && (status == 'M' || 'm'))
return ((income - 1515)*0.15 + 187.15);
else
return ((income - 7624)*0.30 + 2020.42);
}
/*****************************************************/
float FICA(float gross)
{
const float FICAtax = 0.0565;
return (gross*FICAtax);
}
/*****************************************************/
void SendData(ofstream & Outfile, string firstName, string lastName, float hours, float wage, float net, float gross, float tax, float hold)
{
Outfile.open("pay.txt");
Outfile << fixed << showpoint << setprecision(2);
Outfile << "Payroll for: " << firstName << lastName << endl;
Outfile << "Hours worked: " << hours << endl;
Outfile << "Hourly wage: " << wage << endl;
Outfile << "Gross pay: " << gross << endl;
Outfile << "FICA Tax: " << tax << endl;
Outfile << "Federal Income Tax Witheld: " << hold << endl;
Outfile << "Net Amount: " << net << endl;
Outfile << endl;
Outfile.close();
}
|