Code:
#include <iostream>
#include <iomanip>
#include <fstream> // Moved this from two lines below
using namespace std;
void ProcessPayroll(float&, float&, float&, float&, float&, ifstream&, ofstream&);
int ProcessEmployee(int&, float&, int&, ifstream&);
void PrintSummary(float, float, float, float, float, ofstream&);
int main()
{
ifstream infile;
infile.open ("lab1.txt");
if(infile.fail())
{
cout << "failing to open input file\n";
getchar();
exit(1);
}
ofstream outfile;
outfile.open ("out1.txt");
if(outfile.fail())
{
cout << "failing to open output file\n";
getchar();
exit(1);
}
float totgross, totss, totfed, totstate, totnet;
// Added line below, should initialize values before you use them
totgross = totss = totfed = totstate = totnet = 0.0f;
ProcessPayroll(totgross, totss, totfed, totstate, totnet, infile, outfile);
getchar();
return 0;
}
int ProcessEmployee(int& EmpNum, float& Rate, int& Hours, ifstream& input)
{
if(input >> EmpNum >> Rate >> Hours)
return 1;
else
return 0;
}
void ProcessPayroll(float& totgross, float& totss, float& totfed, float& totstate,
float& totnet, ifstream& input, ofstream& outfile)
{
float Rate;
int EmpNum, Hours;
char flag = ' ';
// New variables added
float gross, net, ss, state, fed;
cout << "Employee" << " " << "Hours" << " " << " Rate" << " " << " Gross" << " "
<< " Net" << " " << " Fed" << " " << "State" << " " << "Soc Sec\n";
outfile << "Employee" << " " << "Hours" << " " << " Rate" << " " << " Gross" << " "
<< " Net" << " " << " Fed" << " " << "State" << " " << "Soc Sec\n";
while(ProcessEmployee(EmpNum, Rate, Hours, input))
{
if(Hours > 40)
{
gross = ((Rate * 1.5)*(Hours - 40)) + (Rate * 40);
flag = '$';
}
else if(Hours > 35)
{
gross = Rate * Hours;
flag = ' ';
}
else if(Hours <= 35)
{
gross = (Rate + .15) * Hours;
flag = '*';
}
ss = gross * 0.07;
fed = gross * 0.16;
state = gross * 0.0542;
net = gross - state - fed - state;
totss += ss;
totfed += fed;
totstate += state;
totnet += net;
totgross += gross;
cout << setw(8) << EmpNum << " "
<< setw(5) << Hours << flag << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << gross << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << net << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << fed << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << state << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << ss << "\n";
outfile << setw(8) << EmpNum << " "
<< setw(5) << Hours << flag << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << gross << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << net << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << fed << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << state << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << ss << "\n";
}
PrintSummary(totgross, totss, totfed, totstate, totnet, outfile);
return;
}
void PrintSummary(float totgross, float totss, float totfed, float totstate, float totnet,
ofstream& outfile)
{
//float totgross, totss, totfed, totstate, totnet;
cout << "\n\n\nSummary - Totals For All Employees" << "\n\n "
<< "Gross Pay" << " " << "Net Pay" << " " << "Federal Tax" << " "
<< "State Tax" << " " << "Soc Security\n";
outfile << "\n\n\nSummary - Totals For All Employees" << "\n\n "
<< "Gross Pay" << " " << "Net Pay" << " " << "Federal Tax" << " "
<< "State Tax" << " " << "Soc Security\n";
// Get rid of all this below in blue
//while(ProcessPayroll(totgross, totss, totfed, totstate, totnet, input, outfile))
{
totgross=totgross+totgross;
totnet=totnet+totnet;
totfed=totfed+totfed;
totstate=totstate+totstate;
totss=totss+totss;
cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
outfile << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << " "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
// Get rid of the following in blue
}
outfile.close();
return;
}
Note, your