I've been working on a piece of code for a while, and it works as it is intended to. Now, though, I'm trying to the code down so that there is one main functions and every "action" is a function that is called. But, I have very little experience with functions. I have been reading up and practicing with them, but am still a little bit lost. What would you recommend doing and how would you go about breaking the code below into functions, so that it still works the same way?
Step 3 - Output:
* Student information (name, class sections, assignment/date)
* Program title/mission statement
1) Detailed individual employee reports in page format. (Stored in output file #2 and not displayed on monitor):
Print information until all information is listed, or print "Bad Data...(reason)" after file input data is invalid.
- Print the employee's information.
- Name.
- Gross Salary.
- Medical Benefits.
- Retirement Contribution in percent.
- Company contribution in dollars.
- Employee contribution in dollars.
- Total contributions in dollars.
- Taxes Owed.
- Net Salary.
2) Payroll report in tabular form. (Stored in output file #1 and displayed on monitor):
For each individual employee in the information output table.
- Print each employee's data on one line until all information (below) is listed:
*Note - End output and print "Bad Data...(reason)" if file input data is invalid.
- Name.
- Gross Salary.
- Benefits.
- Contributions.
- Taxes.
- Net Salary.
- Print a complete company pay report:
- Total Gross Salaries paid.
- Total Benefits collected.
- Total Contributions to Retirement Funds.
- Total Taxes Collected.
- Total Net Salaries Paid.
************************ End of Algorithm *****************************/
/************ Code segment *******************************************/
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
int skill, benefitType, taxRemainder;
double hours, contPercent, gross, benefitCost, grossDed, contAmount, taxOwed, netSalary, contTotal, taxID, skillPay;
double sumGross = 0, sumBenefit = 0, sumContributions = 0, sumTaxes = 0, sumNet = 0;
string name, benefitName;
string fileInput1, fileInput2, fileOutput1, fileOutput2;
ifstream fin, fin1;
ofstream fout1, fout2;
// Program title.
cout << " \t Assignment #5: Company Payroll and Employee Reports." << endl;
// Program mission statement.
cout << "This program will prompt user for a data file with a list of a small company's employees, " << endl;
cout << "and will process the data and generate (1) a payroll report and (2) detailed invidiual employee reports." << endl;
cout << "--------------------------------------------------------------" << endl << endl;
cout << "Enter input file name for detailed employee reports in page format: ";
getline(cin, fileInput1);
fin.open(fileInput1.c_str());
if (fin.fail())
{
cout << "Bad input file." << endl << "Program stopped." << endl;
return 0;
}
else
{
cout << "Enter output file name for detailed employee reports in page format: ";
getline(cin, fileOutput1);
fout1.open(fileOutput1.c_str()) ;
fout1 << "Detailed Employee Reports:" << endl;
fin >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin, name);
while (!fin.eof())
{
if (skill == 1)
skillPay = 15.00;
else if (skill == 2)
skillPay = 25.00;
else if (skill == 3)
skillPay = 72.00;
else if (skill == 4)
skillPay = 125.00;
if (hours <= 40)
gross = hours * skillPay;
else if (hours <= 50)
gross = (40 * skillPay) + ((hours - 40) * (skillPay * 1.5));
else if (hours <= 60)
gross = (40 * skillPay) + (10 * (skillPay * 1.5)) + ((hours - 50) * (skillPay * 2));
if (skill == 1 || benefitType == 0)
benefitCost = 0;
else if (benefitType == 1)
benefitCost = 32.50;
else if (benefitType == 2)
benefitCost = 52.50;
else if (benefitType == 3)
benefitCost = 62.50;
contAmount = gross * (contPercent / 100.00);
contTotal = 2 * contAmount;
grossDed = gross - contAmount - benefitCost;
taxRemainder = (grossDed - 5000) / 1000;
if (grossDed <= 2000.00)
taxOwed = 0;
else if (grossDed <= 3000.00)
taxOwed = 0.03 * (grossDed - 2000);
else if (grossDed <= 4000.00)
taxOwed = (0.01 * 3000) + (0.05 * (grossDed - 3000));
else if (grossDed <= 5000.00)
taxOwed = (0.01 * 3000) + (0.05 * 1000) + (0.07 * (grossDed - 4000));
else if (grossDed > 5000.00)
taxOwed = (0.01 * 3000) + (0.05 * 1000) + (0.07 * 1000) + (taxRemainder / 100.0) * (grossDed - 5000);
netSalary = grossDed - taxOwed;
if (skill < 1 || skill > 4)
{
fout1 << left << setprecision(2) << fixed << showpoint << setw(25);
fout1 << "------------------------------------------------" << endl;
fout1 << left << setw(25) << "Name:" << left << setw(25) << name << endl;
fout1 << "Bad data." << endl << "Invalid skill level." << endl << "Employee data output terminated." << endl << endl;
}
else if (hours < 0 || hours > 60 )
{
fout1 << left << setprecision(2) << fixed << showpoint << setw(25);
fout1 << "------------------------------------------------" << endl;
fout1 << left << setw(25) << "Name:" << left << setw(25) << name << endl;
fout1 << left << setw(25) << "Bad data." << endl << "Invalid hours." << endl << "Employee data output terminated." << endl << endl;
}
else if (benefitType < 0 || benefitType > 3)
{
fout1 << left << setprecision(2) << fixed << showpoint << setw(25);
fout1 << "------------------------------------------------" << endl;
fout1 << left << setw(25) << "Name:" << left << setw(25) << name << endl;
fout1 << left << setw(25) << "Gross Salary:" << "$" << left << setw(25) << gross << endl;
fout1 << "Bad data." << endl << "Invalid medical benefit code." << endl << "Employee data output terminated." << endl << endl;
}
else if (skill != 4 && contPercent != 0)
{
fout1 << left << setprecision(2) << fixed << showpoint << setw(25);
fout1 << "------------------------------------------------" << endl;
fout1 << left << setw(25) << "Name:" << left << setw(25) << name << endl;
fout1 << left << setw(25) << "Gross Salary:" << "$" << left << setw(25) << gross << endl;
fout1 << left << setw(25) << "Medical Benefits:" << "$" << left << setw(25) << benefitCost << endl;
fout1 << "Bad data." << endl <<"Invalid retirement contributions for skill level." << endl << "Employee data output terminated." << endl << endl;
}
else if (contPercent < 0 || contPercent > 5)
{
fout1 << left << setprecision(2) << fixed << showpoint << setw(25);
fout1 << "------------------------------------------------" << endl;
fout1 << left << setw(25) << "Name:" << left << setw(25) << name << endl;
fout1 << left << setw(25) << "Gross Salary:" << "$" << left << setw(25) << gross << endl;
fout1 << left << setw(25) << "Medical Benefits:" << "$" << left << setw(25) << benefitCost << endl;
fout1 << "Bad data." << endl <<"Invalid retirement contributions for skill level." << endl << "Employee data output terminated." << endl << endl;
}
else
{
fout1 << left << setprecision(2) << fixed << showpoint << setw(25);
fout1 << "------------------------------------------------" << endl;
fout1 << left << setw(25) << "Name:" << left << setw(25) << name << endl;
fout1 << left << setw(25) << "Gross Salary:" << "$" << left << setw(25) << gross << endl;
fout1 << left << setw(25) << "Medical Benefits:" << "$" << left << setw(25) << benefitCost << endl;
fout1 << left << setw(25) << "Retirement Contribution:" << left << contPercent << "%" << endl;
fout1 << left << setw(25) << " Company:" << "$" << left << setw(25) << contAmount << endl;
fout1 << left << setw(25) << " Employee:" << "$" << left << setw(25) << contAmount << endl;
fout1 << left << setw(25) << " Total:" << "$" << left << setw(25) << contTotal << endl;
fout1 << left << setw(25) << "Taxes Owed:" << "$" << left << setw(25) << taxOwed << endl;
fout1 << left << setw(25) << "Net Salary:" << "$" << left << setw(25) << netSalary << endl << endl;
}
fin >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin, name);
}
fout1.close();
}
cout << endl;
if (!fin.fail())
cout << "Detailed employee reports have been printed in " << fileOutput1 << "." << endl << endl;
cout << "Enter input file name for company payroll report. (Can be same as first input file.): ";
getline(cin, fileInput2);
fin1.open(fileInput2.c_str());
if (fin1.fail())
cout << "Bad input file." << endl << "Program stopped." << endl;
else
{
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
cout << "Enter output file name for company payroll report: ";
getline(cin, fileOutput2);
fout2.open(fileOutput2.c_str()) ;
cout << endl;
cout << " Overview " << endl;
cout << "---------------" << endl;
cout << left << setprecision(2) << fixed << showpoint;
cout << left << setw(25) << " Name" << setw(26) << "Gross Salary" << setw(26) << "Benefits";
cout << left << setw(26) << "Contributions" << setw(26) << "Taxes" << setw(26) << "Net Salary" << endl;
fout2 << endl;
fout2 << " Overview " << endl;
fout2 << "---------------" << endl;
fout2 << left << setprecision(2) << fixed << showpoint;
fout2 << left << setw(25) << " Name" << setw(26) << "Gross Salary" << setw(26) << "Benefits";
fout2 << left << setw(26) << "Contributions" << setw(26) << "Taxes" << setw(26) << "Net Salary" << endl;
while (!fin1.eof())
{
cout << left << setw(25) << name;
fout2 << left << setw(25) << name;
while (!fin1.eof())
{
if (skill < 1 || skill > 4)
{
cout << "Bad data. \t \t Invalid skill level." << endl;
fout2 << "Bad data. \t \t Invalid skill level." << endl;
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
break;
}
else if (hours < 0 || hours > 60)
{
cout << "Bad data. \t \t Invalid hours." << endl;
fout2 << "Bad data. \t \t Invalid hours." << endl;
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
break;
}
else if (benefitType < 0 || benefitType > 3)
{
cout << "Bad data. \t \t Invalid benefit code." << endl;
fout2 << "Bad data. \t \t Invalid benefit code." << endl;
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
break;
}
else if (skill == 1 && benefitType != 0)
{
cout << "Bad data. \t \t Invalid benefit type for skill level." << endl;
fout2 << "Bad data. \t \t Invalid benefit type for skill level." << endl;
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
break;
}
else if (skill != 4 && contPercent != 0)
{
cout << "Bad data. \t \t Invalid contribution percent for skill level." << endl;
fout2 << "Bad data. \t \t Invalid contribution percent for skill level." << endl;
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
break;
}
else if (contPercent < 0 || contPercent > 5)
{
cout << "Bad data. \t \t Invalid contribution: " << contPercent << "%" << endl;
fout2 << "Bad data. \t \t Invalid contribution: " << contPercent << "%" << endl;
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
break;
}
else
{
if (skill == 1)
skillPay = 15.00;
else if (skill == 2)
skillPay = 25.00;
else if (skill == 3)
skillPay = 72.00;
else if (skill == 4)
skillPay = 125.00;
if (hours <= 40)
gross = hours * skillPay;
else if (hours <= 50)
gross = (40 * skillPay) + ((hours - 40) * (skillPay * 1.5));
else if (hours <= 60)
gross = (40 * skillPay) + (10 * (skillPay * 1.5)) + ((hours - 50) * (skillPay * 2));
if (skill == 1 || benefitType == 0)
benefitCost = 0;
else if (benefitType == 1)
benefitCost = 32.50;
else if (benefitType == 2)
benefitCost = 52.50;
else if (benefitType == 3)
benefitCost = 62.50;
contAmount = gross * (contPercent / 100.00);
contTotal = 2 * contAmount;
grossDed = gross - contAmount - benefitCost;
taxRemainder = (grossDed - 5000) / 1000;
if (grossDed <= 2000.00)
taxOwed = 0;
else if (grossDed <= 3000.00)
taxOwed = 0.03 * (grossDed - 2000);
else if (grossDed <= 4000.00)
taxOwed = (0.01 * 3000) + (0.05 * (grossDed - 3000));
else if (grossDed <= 5000.00)
taxOwed = (0.01 * 3000) + (0.05 * 1000) + (0.07 * (grossDed - 4000));
else if (grossDed > 5000.00)
taxOwed = (0.01 * 3000) + (0.05 * 1000) + (0.07 * 1000) + (taxRemainder / 100) * (grossDed - 5000);
netSalary = grossDed - taxOwed;
sumGross += gross;
sumBenefit += benefitCost;
sumContributions += contTotal;
sumTaxes += taxOwed;
sumNet += netSalary;
cout << left << setprecision(2) << fixed << showpoint;
cout << "$" << left << setw(25) << gross << "$" << left << setw(25) << benefitCost << "$" << left << setw(25) << contAmount;
cout << "$" << left << setw(25) << taxOwed << "$" << left << setw(25) << netSalary << endl;
fout2 << left << setprecision(2) << fixed << showpoint;
fout2 << "$" << left << setw(25) << gross << "$" << left << setw(25) << benefitCost << "$" << left << setw(25) << contAmount;
fout2 << "$" << left << setw(25) << taxOwed << "$" << left << setw(25) << netSalary << endl;
fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
getline(fin1, name);
break;
}
}
}
cout << endl << endl;
cout << " Totals " << endl;
cout << "-------------" << endl;
cout << left << setprecision(2) << fixed << showpoint;
cout << setw(44) << "Total Gross Salaries paid:" << "$" << left << setw(25) << sumGross << endl;
cout << setw(44) << "Total Benefits collected:" << "$" << left << setw(25) << sumBenefit << endl;
cout << setw(44) << "Total Contributions to Retirement Funds:" << "$" << left << setw(25) << sumContributions << endl;
cout << setw(44) << "Total Taxes collected:" << "$" << left << setw(25) << sumTaxes << endl;
cout << setw(44) << "Total Net Salaries paid:" << "$" << left << setw(25) << sumNet << endl;
fout2 << endl << endl;
fout2 << " Totals " << endl;
fout2 << "-------------" << endl;
fout2 << left << setprecision(2) << fixed << showpoint;
fout2 << setw(44) << "Total Gross Salaries paid:" << "$" << left << setw(25) << sumGross << endl;
fout2 << setw(44) << "Total Benefits collected:" << "$" << left << setw(25) << sumBenefit << endl;
fout2 << setw(44) << "Total Contributions to Retirement Funds:" << "$" << left << setw(25) << sumContributions << endl;
fout2 << setw(44) << "Total Taxes collected:" << "$" << left << setw(25) << sumTaxes << endl;
fout2 << setw(44) << "Total Net Salaries paid:" << "$" << left << setw(25) << sumNet << endl << endl;
fout2.close();
}
return 0;
}
Thank you so much.