Help with Functions

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1079 Views - Last Post: 25 October 2009 - 06:38 PM Rate Topic: -----

#1 swolll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-October 09

Help with Functions

Post icon  Posted 23 October 2009 - 12:07 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Functions

#2 Guest_c.user*


Reputation:

Re: Help with Functions

Posted 23 October 2009 - 03:27 AM

start with outputs

a function must get the filestream and the variable or the variables for the changing an output

all messages inside the function
one function is for the one group of messages
second function is for another group
third ...

then separate your big ifs to small ifs (for the preparing them for their functions)
Was This Post Helpful? 0

#3 swolll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-October 09

Re: Help with Functions

Posted 23 October 2009 - 08:36 PM

Thanks guys. I've been working on it all night. I'm a bit stumped, and was wondering:
1) Am I on the right track, and is it just small details to correct now? Or will I need to overhaul my code (and how?)?
2) My compiler gives me a bunch of syntax errors (especially primary-expression...) that I don't know how to fix. Any help would be great.

Here's the new code, and thanks so much:
 #include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

// Function Prototypes
void Program_Info (void);		
void Open_File_1(ifstream &fin);
void Output_File_1 (ofstream &fout1);
void Read_Data_1 (ifstream &fin, int skill, int benefitType, double contPercent, double hours, double taxID, string name);
double Skill_Pay (int skill);
double Gross_Pay (double skillPay, double hours);
double Benefit_Cost (int skill, int benefitType);
void Gross_Calculations (double gross, double contPercent, double &contAmount, double &grossDed, int &taxRemainder);
void Taxes_and_Net (double grossDed, int taxRemainder, double &taxOwed, double &netSalary);
void Print_File_1 (ofstream&, int skill, string name, double hours, int benefitType, double gross, double contPercent, double benefitCost, double contAmount, double contTotal, double taxOwed, double netSalary);

void Open_File_2(ifstream &fin1);
void Output_File_1 (ofstream &fout2);
void Read_Data_2 (ifstream &fin1, int skill, int benefitType, double contPercent, double hours, double taxID, string name);
void Print_Head_2 (ofstream &fout2);
void Sum_Calculations (double gross, double benefitCost, double contTotal, double taxOwed, double netSalary, double &sumGross, double &sumBenefit, double &sumContributions, double &sumTaxes, double &sumNet);
void Print_Employee_2 (ofstream &fout2, int skill, double hours, int benefitType, double contPercent, double gross, double benefitCost, double contAmount, double taxOwed, double netSalary);
void Print_Totals_2 (ofstream &fout2, double sumGross, double sumBenefit, double sumContributions, double sumTaxes, double sumNet);

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_Info();
	Open_File_1(fin);
	Output_File_1(fout1);
	
	fout1 << "Detailed Employee Reports:" << endl;

	Read_Data_1(fin, skill, benefitType, contPercent, hours, taxID, name);
	
	while (!fin.eof())
	{
		Skill_Pay (skill);
		Gross_Pay (skillPay, hours);
		Benefit_Cost (skill, benefitType);
		Gross_Calculations (gross, contPercent, contAmount, grossDed, taxRemainder);
		Taxes_and_Net (grossDed, taxRemainder, taxOwed, netSalary);

		Print_File_1 (skill, name, hours, benefitType, gross, contPercent, benefitCost, contAmount, contTotal, taxOwed, netSalary);		

		Read_Data_1(fin, skill, benefitType, contPercent, hours, taxID, name);
	}   
  
	fout1.close();
}
	
	if (!fin.fail())		
		cout << endl << "Detailed employee reports have been printed in " << fileOutput1 << "." << endl << endl;

	Open_File_2(fin1);
	Output_File_2(fout2);

	Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
	Print_Head_2 (ofstream &fout2);

	while (!fin1.eof())	
	{
		cout << left << setw(25) << name;
		fout2 << left << setw(25) << name;
			while (!fin1.eof())
			{
				Print_Employee_2 (skill, hours, benefitType, contPercent, gross, benefitCost, contAmount, taxOwed, netSalary);
				Sum_Calculations (gross, benefitCost, contTotal, taxOwed, netSalary, &sumGross, &sumBenefit, &sumContributions, &sumTaxes, &sumNet)
				Print_Employee_2 (fout2, skill, hours, benefitType, contPercent, gross, benefitCost, contAmount, taxOwed, netSalary);
				break;
			}
	}
	
	Print_Totals_2 (fout2, sumGross, sumBenefit, sumContributions, sumTaxes, sumNet);		

	fout2.close();
	}
	
	return 0;
}


// Function Declarations
 // Individual Reports
void Program_Info (void)
{
	cout << endl;
	cout << "--------------------------------------------------------------" << endl;	
	cout << "Student Name and ID:	 xxx" << endl;
	cout << "Course Section:		  xxx" << endl;
	cout << "Lab Section:			 xxx" << endl;
	cout << "--------------------------------------------------------------" << endl;
  
	// 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;
}
	 
void Open_File_1(ifstream &fin)
{
	string fileInput1;
	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;
	}
}

void Output_File_1 (fout1)
{
	string fileOutput1;	
	cout << "Enter output file name for detailed employee reports in page format: ";
	getline(cin, fileOutput1);					

	fout1.open(fileOutput1.c_str());
}	
		
void Read_Data_1 (ifstream &fin, int skill, int benefitType, double contPercent, double hours, double taxID, string name);
{
	fin >> skill >> benefitType >> contPercent >> hours >> taxID;
	getline(fin, name);	
}
		
double Skill_Pay (int skill);
{
	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;	
	
	return (skillPay);
}

double Gross_Pay (int skillPay, double hours);
{
	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));

	return (gross);
}

double Benefit_Cost (int skill, int benefitType);
{
	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;
		
	return (benefitCost);	
}

void Gross_Calculations (double gross, double contPercent, double &contAmount, double &grossDed, double &taxRemainder);
{
	contAmount = gross * (contPercent / 100.00);
	contTotal = 2 * contAmount;
	grossDed = gross - contAmount - benefitCost;
	taxRemainder = (grossDed - 5000) / 1000;
}

void Taxes_and_Net (double grossDed, double taxRemainder, double &taxOwed, double &netSalary);
{
	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;
}

void Print_File_1 (ofstream &fout1, int skill, string name, double hours, int benefitType, double gross, double contPercent, double benefitCost, double contAmount, double contTotal, double taxOwed, double netSalary);	
{
	  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;	
	}
}
			
 // Payroll Report
void Open_File_2(ifstream &fin1)
{
	string fileInput2;
	
	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;
		return 0;
	}	
}

void Output_File_2 (fout2)
{
	string fileOutput1;	
	cout << "Enter output file name for company payroll report: ";	
	getline(cin, fileOutput2);
	
	fout2.open(fileOutput2.c_str());
}	
				
void Read_Data_2 (ifstream &fin1, int skill, int benefitType, double contPercent, double hours, double taxID, string name);
{
	fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
	getline(fin1, name);	
}
 
void Print_Head_2 (ofstream &fout2);
{
	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;	
}	

void Print_Employee_2 (int skill, double hours, int benefitType, double contPercent, double gross, double benefitCost, double contAmount, double taxOwed, double netSalary);
{
	if (skill < 1 || skill > 4)
	{
		cout << "Bad data. \t \t Invalid skill level." << endl;
		fout2 << "Bad data. \t \t Invalid skill level." << endl;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
		break;
	}
	else if (hours < 0 || hours > 60)
	{
		cout << "Bad data. \t \t Invalid hours." << endl;
		fout2 << "Bad data. \t \t Invalid hours." << endl;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
		break;
	}
	else
	{
		Skill_Pay (skill);
		Gross_Pay (skillPay, hours);
		Benefit_Cost (skill, benefitType);
		Gross_Calculations (gross, contPercent, &contAmount, &grossDed, &taxRemainder);
		Taxes_and_Net (grossDed, taxRemainder, &taxOwed, &netSalary);
	}
}
	
void Sum_Calculations (int gross, int benefitCost, double contTotal, double taxOwed, double netSalary, double &sumGross, double &sumBenefit, double &sumContributions, double &sumTaxes, double &sumNet)
{
	sumGross += gross;
	sumBenefit += benefitCost;
	sumContributions += contTotal;
	sumTaxes += taxOwed;
	sumNet += netSalary;	
}

void Print_Employee_2 (ofstream &fout2, int skill, double hours, int benefitType, double contPercent, double gross, double benefitCost, double contAmount, double taxOwed, double 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; 
	
	Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
}

void Print_Totals_2 (ofstream &fout2, double sumGross, double sumBenefit, double sumContributions, double sumTaxes, double sumNet)
{
	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;	
}


Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with Functions

Posted 23 October 2009 - 09:22 PM

What do you believe eof(), e.g. fin.eof() tells you? How do you know or why do you believe this?
Was This Post Helpful? 0
  • +
  • -

#5 swolll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-October 09

Re: Help with Functions

Posted 23 October 2009 - 11:55 PM

View PostOler1s, on 23 Oct, 2009 - 08:22 PM, said:

What do you believe eof(), e.g. fin.eof() tells you? How do you know or why do you believe this?


I'm under the assumption that eof means end of file. So, when I use eof, I'm hoping the program to read from a file until there is no data left in it , and then stop.

Any advice, even in the slightest, is appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with Functions

Posted 24 October 2009 - 12:53 AM

Quote

I'm under the assumption that eof means end of file.
That's what "eof" stands for, yes.

Quote

So, when I use eof, I'm hoping the program to read from a file until there is no data left in it , and then stop.
Don't hope. Don't assume. Verify. What does eof(), the function return? You need to check some documentation source to know, right? I mean, you don't intend to guess what functions do.

Quote

Any advice, even in the slightest, is appreciated.
I just gave you my first piece of advice then. Don't assume. Don't guess. Don't hope. Test, observe, and verify.
Was This Post Helpful? 0
  • +
  • -

#7 swolll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-October 09

Re: Help with Functions

Posted 24 October 2009 - 01:25 AM

View PostOler1s, on 23 Oct, 2009 - 11:53 PM, said:

Quote

I'm under the assumption that eof means end of file.
That's what "eof" stands for, yes.

Quote

So, when I use eof, I'm hoping the program to read from a file until there is no data left in it , and then stop.
Don't hope. Don't assume. Verify. What does eof(), the function return? You need to check some documentation source to know, right? I mean, you don't intend to guess what functions do.

Quote

Any advice, even in the slightest, is appreciated.
I just gave you my first piece of advice then. Don't assume. Don't guess. Don't hope. Test, observe, and verify.


You've lost me.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_c.user*


Reputation:

Re: Help with Functions

Posted 24 October 2009 - 02:19 AM

void Open_File_2(ifstream &fin1)


function returns nothing, but there is a return inside with a number

	if (fin1.fail())			
	{
		cout << "Bad input file." << endl << "Program stopped." << endl;
		return 0;
	} 



while you are starting with them, use a simple form zero or one for the return and the int type (you can use the bool type also)

the way is right

void Print_File_1 (ofstream&, int skill, string name, double hours, int benefitType, double gross, double contPercent, double benefitCost, double contAmount, double contTotal, double taxOwed, double netSalary);


for such long variable lists use a structure, or several structures

This post has been edited by c.user: 24 October 2009 - 02:17 AM

Was This Post Helpful? 0

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with Functions

Posted 24 October 2009 - 02:20 AM

Quote

You've lost me.
You want your program to read from the file until the end, and stop. That's what you want. (Obviously I'm bringing this up because I see you made a mistake). But what I want you to be able to discover your mistake.

Would you agree that C++ is a language with rules? The program does not follow what you want, but the translation by the C++ rules, of what you wrote? That you need to verify that you what you wrote does what you want?

Consider the line while (!fin.eof()). How does C++ handle this particular line? First, fin.eof() must be evaluated. eof() returns a value, with a certain return type. For example:

int addTwo(int a, int B ); returns an integer. In terms of logical meaning, it is the sum of the two arguments to the function. Always.

eof() is a function. It returns something. I want you to tell me (a) what is the type of this returned value ( B ) what value it takes and under what conditions. You can answer both questions by checking documentation like sources.

This post has been edited by Oler1s: 24 October 2009 - 02:21 AM

Was This Post Helpful? 0
  • +
  • -

#10 swolll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-October 09

Re: Help with Functions

Posted 24 October 2009 - 10:04 AM

View PostOler1s, on 24 Oct, 2009 - 01:20 AM, said:

Quote

You've lost me.
You want your program to read from the file until the end, and stop. That's what you want. (Obviously I'm bringing this up because I see you made a mistake). But what I want you to be able to discover your mistake.

Would you agree that C++ is a language with rules? The program does not follow what you want, but the translation by the C++ rules, of what you wrote? That you need to verify that you what you wrote does what you want?

Consider the line while (!fin.eof()). How does C++ handle this particular line? First, fin.eof() must be evaluated. eof() returns a value, with a certain return type. For example:

int addTwo(int a, int B ); returns an integer. In terms of logical meaning, it is the sum of the two arguments to the function. Always.

eof() is a function. It returns something. I want you to tell me (a) what is the type of this returned value ( B ) what value it takes and under what conditions. You can answer both questions by checking documentation like sources.


It returns a 0, causing the function to stop right? So, data type would be int?

Is my problem with the use of eof? Because that is the method my teacher has taught and expects us to use.
Was This Post Helpful? 0
  • +
  • -

#11 swolll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-October 09

Re: Help with Functions

Posted 24 October 2009 - 11:51 AM

Thanks, dudes. I'm going to work at it for an hour or two, and post back with any questions.

A few things to start, though:
1) I'm a bit confused as to when to use/declare global variables and when to use/declare local variables. I understand their scopes, but do you usually use one for passing by value and the other for passing by reference?

2) To be clear, is it:
Function Prototype: datatype functionname (parameter list with types and identifiers of what is to be passed; include any & for references);

Function Call: functionname (parameter list with only names of identifiers; no &'s or datatype);

Function Definition: datatype functionname (paramter list with data type and name received from main; include &'s for references)

3) Can you tell if my file streams (input and output) and reading them are okay?
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with Functions

Posted 24 October 2009 - 12:32 PM

Quote

It returns a 0, causing the function to stop right?
It returns whatever eofbit is set to. eofbit is a flag in the stream class, so the question is what sets this flag?

Quote

So, data type would be int?
No. A bool. I'm not aware of any documentation source that says the data type is an int. Were you guessing here? There are ways to determine what the datatype is, so if you don't know how, you need to ask...not guess.

Quote

Is my problem with the use of eof?
Yes, you use eof like it can predict the future.

Quote

Because that is the method my teacher has taught and expects us to use.
He or she is wrong. The eof idiom is dead wrong, because you will see incorrect behavior in your program. Thus, people figure out one of two things. They either don't study their use of eof and hack their code to deal with their mistake, or fix the mistake itself.

When you have a stream, if you want to find out what is there further in the stream, you need to do something called "reading". Seems to make sense. Let's say there are 50 characters in the stream. You have read 20 so far. That means there are 30 left. How do you find out there are 30 left? Since you want to know something about the stream that is further in the stream, you need to read.

So let's say yoy read 50 characters in the stream. There are no characters left. You are at the end of the stream. How do you discover that there is nothing more left in the stream? Well, since you want information about something further in the stream, you need to read. Ok, how does this work? Reading is an operation that is not guaranteed to success. Like many operations, reading can fail. So after every read, you need to check for failure! What are some read operations. The >> operator. getline. get. And so on. After every read, you can have failure. You could suddenly reach an eof after a read.

So what do you think goes wrong when you check for eof first, and then read from the stream?

Quote

I'm a bit confused as to when to use/declare global variables and when to use/declare local variables. I understand their scopes, but do you usually use one for passing by value and the other for passing by reference?
No, that's not it at all. Passing by value and passing by reference isn't connected to global variables. You are in fact free to pass global variables around, although that defeats the purpose of a global variable.

You generally don't want to use global variables. So the idea of a global variable is that every single function can read and write to it. It's global. It's usually a bad idea to have global variables around. Instead, you want to pass variables around as needed.

It's not an issue that's obvious in beginner programs, but as you write larger and larger programs, maintainability and other concerns like parallelism become a major issue. Global variables can quickly cause problems for either.

Quote

Function Prototype: datatype functionname (parameter list with types and identifiers of what is to be passed; include any & for references);
Technically speaking, a "prototype" is what is known as a function declaration, and it's a bit more involved than what you typed. But yes, but you typed is somewhat it. You don't need the identifiers though. int SomeFunction(int, int) is a perfectly fine function declaration.


Quote

Function Call: functionname (parameter list with only names of identifiers; no &'s or datatype);
Yes, although we call what is supplied in between the parenthesis arguments.

Quote

Function Definition: datatype functionname (paramter list with data type and name received from main; include &'s for references)
Sort of. The identifiers for the parameter are not linked to the identifiers of the arguments. It has nothing to do with main. It's the idea of a function. You have this arbitrary function, and you give your own name to the arguments. It's independent of whatever function calls it.

int addTwo(int, int);
int addFunny(int a, b) { return addTwo(a,b)+1; }
int main() { addTwo(1,5); int a=7, b=9; addTwo(a,b); addFunny(a,b); }
int addTwo(int a, int b) { return a+b}



There's a and b in main. That's the name of variables local to main. There's a and b in addTwo, and addFunny. They are all completely separate from one another. Remember, they exist in completely different scopes. The a and b in main are in the function scope of main.
Was This Post Helpful? 0
  • +
  • -

#13 swolll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-October 09

Re: Help with Functions

Posted 24 October 2009 - 04:43 PM

This is tedious. And I'm a bit desperate. I still can't fix a numbers of errors, despite working at this since early this morning. And ideas?

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

// Function Prototypes
void Program_Info (void);		
void Open_File_1(ifstream &fin);
void Output_File_1 (ofstream &fout1);
void Read_Data_1 (ifstream &fin, int skill, int benefitType, double contPercent, double hours, double taxID, string name);
double Skill_Pay (int skill);
double Gross_Pay (double skillPay, double hours);
double Benefit_Cost (int skill, int benefitType);
void Gross_Calculations (double gross, double contPercent, double &contAmount, double &contTotal, double &benefitCost, double &grossDed, int &taxRemainder);
void Taxes_and_Net (double grossDed, int taxRemainder, double &taxOwed, double &netSalary);

void Print_File_1 (ofstream &fout1, int skill, string name, double hours, int benefitType, double gross, double contPercent, double benefitCost, double contAmount, double contTotal, double taxOwed, double netSalary);
void Open_File_2(ifstream &fin1);
void Output_File_2 (ofstream &fout2);
void Read_Data_2 (ifstream &fin1, int skill, int benefitType, double contPercent, double hours, double taxID, string name);
void Print_Head_2 (ofstream &fout2);
void Sum_Calculations (double gross, double benefitCost, double contTotal, double taxOwed, double netSalary, double &sumGross, double &sumBenefit, double &sumContributions, double &sumTaxes, double &sumNet);
void Print_Employee_2 (ofstream &fout2, int skill, double hours, int benefitType, double contPercent, double gross, double benefitCost, double contAmount, double taxOwed, double netSalary);
void Print_Totals_2 (ofstream &fout2, double sumGross, double sumBenefit, double sumContributions, double sumTaxes, double sumNet);

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_Info();
		Open_File_1(fin);
		Output_File_1(fout1);
	   
	fout1 << "Detailed Employee Reports:" << endl;

	Read_Data_1(fin, skill, benefitType, contPercent, hours, taxID, name);
	
	while (!fin.eof())
	{
		Skill_Pay (skill);
		Gross_Pay (skillPay, hours);
		Benefit_Cost (skill, benefitType);
		Gross_Calculations (gross, contPercent, contAmount, contTotal, benefitCost, grossDed, taxRemainder);
		Taxes_and_Net (grossDed, taxRemainder, taxOwed, netSalary);

		Print_File_1(fout1, skill, name, hours, benefitType, gross, contPercent, benefitCost, contAmount, contTotal, taxOwed, netSalary);		

		Read_Data_1(fin, skill, benefitType, contPercent, hours, taxID, name);
	}   
		   fout1.close();
	   
	if (!fin.fail())		
		cout << "Detailed employee reports have been printed." << endl << endl;

	Open_File_2(fin1);
	Output_File_2(fout2);

	Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
	Print_Head_2 (fout2);

	while (!fin1.eof())	
	{
		cout << left << setw(25) << name;
		fout2 << left << setw(25) << name;
			while (!fin1.eof())
			{
				Print_Employee_2 (fout2, skill, hours, benefitType, contPercent, gross, benefitCost, contAmount, taxOwed, netSalary);
				Sum_Calculations (gross, benefitCost, contTotal, taxOwed, netSalary, sumGross, sumBenefit, sumContributions, sumTaxes, sumNet);
				Print_Employee_2 (fout2, skill, hours, benefitType, contPercent, gross, benefitCost, contAmount, taxOwed, netSalary);
				break;
			}
	}
	
	Print_Totals_2 (fout2, sumGross, sumBenefit, sumContributions, sumTaxes, sumNet);		

	fout2.close();
	
	return 0;
}

// Function Declarations
 // Individual Reports
void Program_Info (void)
{
	cout << endl;
	cout << "--------------------------------------------------------------" << endl;	
	cout << "Student Name and ID:	 ss7" << endl;
	cout << "Course Section:		  ss01" << endl;
	cout << "Lab Section:			ss05" << endl;
	cout << "--------------------------------------------------------------" << endl;
  
	// 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;
}
  
void Open_File_1(ifstream &fin)
{
	string fileInput1;
	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;
		exit(1);
	}
}

void Output_File_1 (ofstream &fout1)
{
	string fileOutput1;	
	cout << "Enter output file name for detailed employee reports in page format: ";
	getline(cin, fileOutput1);					

	fout1.open(fileOutput1.c_str());
}	
		
void Read_Data_1 (ifstream &fin, int skill, int benefitType, double contPercent, double hours, double taxID, string name)
{
	fin >> skill >> benefitType >> contPercent >> hours >> taxID;
	getline(fin, name);	
}
		
double Skill_Pay (int skill)
{
  double skillPay;
	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;	
	
	return (skillPay);
}

double Gross_Pay (int skillPay, double hours)
{
  double gross;
	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));

	return (gross);
}

double Benefit_Cost (int skill, int benefitType)
{
  double benefitCost;
	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;
		
	return (benefitCost);	
}

void Gross_Calculations (double gross, double contPercent, double &contAmount, double &contTotal, double benefitCost, double &grossDed, int &taxRemainder)
{
	
	contAmount = gross * (contPercent / 100.00);
	contTotal = 2 * contAmount;
	grossDed = gross - contAmount - benefitCost;
	taxRemainder = (grossDed - 5000) / 1000;
}

void Taxes_and_Net (double grossDed, int taxRemainder, double &taxOwed, double &netSalary)
{
	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;
}

void Print_File_1 (ofstream &fout1, int skill, string name, double hours, int benefitType, double gross, double contPercent, double benefitCost, double contAmount, double contTotal, double taxOwed, double netSalary)	
{
	  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;	
	}
}
			
 // Payroll Report
void Open_File_2(ifstream &fin1)
{
	string fileInput2;
	
	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;
		exit(1);
	}	
}

void Output_File_2 (fout2)
{
	string fileOutput1;	
	cout << "Enter output file name for company payroll report: ";	
	getline(cin, fileOutput2);
	
	fout2.open(fileOutput2.c_str());
}	
				
void Read_Data_2 (ifstream &fin1, int skill, int benefitType, double contPercent, double hours, double taxID, string name)
{
	fin1 >> skill >> benefitType >> contPercent >> hours >> taxID;
	getline(fin1, name);	
}
 
void Print_Head_2 (ofstream &fout2)
{
	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;	
}	

void Print_Employee_2 (int skill, double hours, int benefitType, double contPercent, double gross, double benefitCost, double contAmount, double taxOwed, double netSalary)
{
	if (skill < 1 || skill > 4)
	{
		cout << "Bad data. \t \t Invalid skill level." << endl;
		fout2 << "Bad data. \t \t Invalid skill level." << endl;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
		break;
	}
	else if (hours < 0 || hours > 60)
	{
		cout << "Bad data. \t \t Invalid hours." << endl;
		fout2 << "Bad data. \t \t Invalid hours." << endl;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, 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;

		Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
		break;
	}
	else
	{
		Skill_Pay (skill);
		Gross_Pay (skillPay, hours);
		Benefit_Cost (skill, benefitType);
		Gross_Calculations (gross, contPercent, &contAmount, &grossDed, &taxRemainder);
		Taxes_and_Net (grossDed, taxRemainder, &taxOwed, &netSalary);
	}
}
	
void Sum_Calculations (int gross, int benefitCost, double contTotal, double taxOwed, double netSalary, double &sumGross, double &sumBenefit, double &sumContributions, double &sumTaxes, double &sumNet)
{
	sumGross += gross;
	sumBenefit += benefitCost;
	sumContributions += contTotal;
	sumTaxes += taxOwed;
	sumNet += netSalary;	
}

void Print_Employee_2 (ofstream &fout2, int skill, double hours, int benefitType, double contPercent, double gross, double benefitCost, double contAmount, double taxOwed, double 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; 
	
	Read_Data_2(fin1, skill, benefitType, contPercent, hours, taxID, name);
}

void Print_Totals_2 (ofstream &fout2, double sumGross, double sumBenefit, double sumContributions, double sumTaxes, double sumNet)
{
	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;	
}






Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,505
  • Joined: 19-February 09

Re: Help with Functions

Posted 24 October 2009 - 05:47 PM

What are the errors?
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Help with Functions

Posted 24 October 2009 - 07:05 PM

Are these them?
tedious.cpp:282: error: variable or field ‘Output_File_2’ declared void
tedious.cpp:282: error: ‘fout2’ was not declared in this scope
tedious.cpp: In function ‘void Print_Employee_2(int, double, int, double, double, double, double, double, double)’:
tedious.cpp:319: error: ‘fout2’ was not declared in this scope
tedious.cpp:321: error: ‘fin1’ was not declared in this scope
tedious.cpp:321: error: ‘taxID’ was not declared in this scope
tedious.cpp:321: error: ‘name’ was not declared in this scope
tedious.cpp:322: error: break statement not within loop or switch
tedious.cpp:327: error: ‘fout2’ was not declared in this scope
tedious.cpp:329: error: ‘fin1’ was not declared in this scope
tedious.cpp:329: error: ‘taxID’ was not declared in this scope
tedious.cpp:329: error: ‘name’ was not declared in this scope
tedious.cpp:330: error: break statement not within loop or switch
tedious.cpp:335: error: ‘fout2’ was not declared in this scope
tedious.cpp:337: error: ‘fin1’ was not declared in this scope
tedious.cpp:337: error: ‘taxID’ was not declared in this scope
tedious.cpp:337: error: ‘name’ was not declared in this scope
tedious.cpp:338: error: break statement not within loop or switch
tedious.cpp:343: error: ‘fout2’ was not declared in this scope
tedious.cpp:345: error: ‘fin1’ was not declared in this scope
tedious.cpp:345: error: ‘taxID’ was not declared in this scope
tedious.cpp:345: error: ‘name’ was not declared in this scope
tedious.cpp:346: error: break statement not within loop or switch
tedious.cpp:351: error: ‘fout2’ was not declared in this scope
tedious.cpp:353: error: ‘fin1’ was not declared in this scope
tedious.cpp:353: error: ‘taxID’ was not declared in this scope
tedious.cpp:353: error: ‘name’ was not declared in this scope
tedious.cpp:354: error: break statement not within loop or switch
tedious.cpp:359: error: ‘fout2’ was not declared in this scope
tedious.cpp:361: error: ‘fin1’ was not declared in this scope
tedious.cpp:361: error: ‘taxID’ was not declared in this scope
tedious.cpp:361: error: ‘name’ was not declared in this scope
tedious.cpp:362: error: break statement not within loop or switch
tedious.cpp:367: error: ‘skillPay’ was not declared in this scope
tedious.cpp:369: error: ‘grossDed’ was not declared in this scope
tedious.cpp:369: error: ‘taxRemainder’ was not declared in this scope
tedious.cpp: In function ‘void Print_Employee_2(std::ofstream&, int, double, int, double, double, double, double, double, double)’:
tedious.cpp:393: error: ‘fin1’ was not declared in this scope
tedious.cpp:393: error: ‘taxID’ was not declared in this scope
tedious.cpp:393: error: ‘name’ was not declared in this scope



First one, you defined your function:
void Output_File_2 (fout2)

Something's missing there, eh? In the argument?

Secondly,
tedious.cpp: In function ‘void Print_Employee_2(int, double, int, double, double, double, double, double, double)’:

you appear to be missing arguments for fout2, fin1, taxID and name, so it can't find them inside the function.

tedious.cpp:354: error: break statement not within loop or switch

Pretty self-explanatory there. break is only a valid statement when inside a loop or a switch. Perhaps you meant return?

I'll let you work on those.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2