New Question. I end the output to one file (fout1) and try to start it to a new input and output file, using this code:

" fout1.close();
}

cout << endl << "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.): " << endl;	
getline(fin, fileInput2);
fin.open(fileInput2.c_str());

if (fin.fail())	
cout << "Bad input file." << endl << "Program stopped." << endl;
else
{
fin >> skill >> benefitType >> contPercent >> hours;
getline(fin, taxID);
getline(fin, name);

cout << "Enter output file name for company payroll report: ";	
cin >> fileOutput2;	
fout2.open(fileOutput2.c_str()) ;

cout << "Name \t \t Gross Salary \t Benefits \t Contributions \t Taxes \t Net Salary" << endl;
fout2 << "Name \t \t Gross Salary \t Benefits \t Contributions \t Taxes \t Net Salary" << endl;

while (!fin.eof())	"

But, when compiled, nothing shows up after the first output stream finishes. Where did I go wrong?

Recommended Answers

All 5 Replies

...
cout << endl << "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.): " << endl;	
getline(fin, fileInput2); // The error lies in this line
fin.open(fileInput2.c_str());

...

I assume that you want to read the user input. But you didn't define where getline should read. So you should say cin.getline(fin, fileInput2) instead of just getline(fin, fileInput2).

That didn't work. Here's my code. Where did I go wrong?

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
	int skill, benefitType, skillPay, taxRemainder;
	double hours, contPercent, gross, benefitCost, grossDed, contAmount, taxOwed, netSalary, contTotal;
	double sumGross = 0, sumBenefit = 0, sumContributions = 0, sumTaxes = 0, sumNet = 0;
	string name, taxID, benefitName;
	string fileInput1, fileInput2, fileOutput1, fileOutput2;

	ifstream fin;
	ofstream fout1, fout2; 

	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;
	else
	{
		cout << "Enter output file name for detailed employee reports in page format: ";	
		cin >> fileOutput1;					
		fout1.open(fileOutput1.c_str()) ;	
		
		fout1 << "Detailed Employee Reports:" << endl;

		fin >> skill >> benefitType >> contPercent >> hours;
		getline(fin, 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);
	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) + (0.07 + (taxRemainder / 100.0)) * (grossDed - 5000);
				
	netSalary = grossDed - taxOwed;
  
  	if (skill < 1 || skill > 4)
	{
		fout1 << left << fixed << setprecision(2) << setw(6);
		fout1 << "------------------------------------------------" << endl;
		fout1 << "Name:                           	 " << name << endl;
		fout1 << "Bad data." << endl << "Invalid skill level." << endl << "Employee data output terminated." << endl << endl;   
	}
	else if (hours < 0 || hours > 60 )
	{
		fout1 << left << fixed << setprecision(2) << setw(6);
		fout1 << "------------------------------------------------" << endl;
		fout1 << "Name:                           	 " << name << endl;
		fout1 << "Bad data." << endl << "Invalid hours." << endl << "Employee data output terminated." << endl << endl;   
	}		
	else if (benefitType < 0 || benefitType > 3)
	{
		fout1 << left << fixed << setprecision(2) << setw(6);
		fout1 << "------------------------------------------------" << endl;
		fout1 << "Name:                           	 " << name << endl;
		fout1 << "Gross Salary:               	    $" << 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 << fixed << setprecision(2) << setw(6);
		fout1 << "------------------------------------------------" << endl;
		fout1 << "Name:                           	 " << name << endl;
		fout1 << "Gross Salary:               	    $" << gross << endl;   
		fout1 << "Medical Benefits:                 $" << 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 << fixed << setprecision(2) << setw(6);
		fout1 << "------------------------------------------------" << endl;
		fout1 << "Name:                           	 " << name << endl;
		fout1 << "Gross Salary:               	    $" << gross << endl;   
		fout1 << "Medical Benefits:                 $" << benefitCost << endl;
		fout1 << "Bad data." << endl <<"Invalid retirement contributions for skill level." << endl << "Employee data output terminated." << endl << endl;   
	}	
	else
	{
		fout1 << left << fixed << setprecision(2) << setw(6);
		fout1 << "------------------------------------------------" << endl;
		fout1 << "Name:                           	 " << name << endl;
		fout1 << "Gross Salary:               	    $" << gross << endl;   
		fout1 << "Medical Benefits:                 $" << benefitCost << endl;
		fout1 << "Retirement Contribution:           " << contPercent << "%" << endl;
		fout1 << "\t Company:   		            $" << contAmount << endl;
		fout1 << "\t Employee:  			        $" << contAmount << endl;
		fout1 << "\t Total:                         $" << contTotal << endl;
		fout1 << "Taxes Owed:  					    $" << taxOwed << endl;
		fout1 << "Net Salary:                       $" << netSalary << endl << endl;	
	}
	
	fin >> skill >> benefitType >> contPercent >> hours;
		getline(fin, taxID);
		getline(fin, name);			
	}   
  fout1.close();

}
	
	cout << endl << "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.): " << endl;	
	getline(fin, fileInput2);
	fin.open(fileInput2.c_str());
	
	if (fin.fail())			
		cout << "Bad input file." << endl << "Program stopped." << endl;
	else
	{
		fin >> skill >> benefitType >> contPercent >> hours;
		getline(fin, taxID);
		getline(fin, name);

		cout << "Enter output file name for company payroll report: ";	
		cin >> fileOutput2;					
		fout2.open(fileOutput2.c_str()) ;
	
	cout << "Name \t \t Gross Salary \t Benefits \t Contributions \t Taxes \t Net Salary" << endl;
	fout2 << "Name \t \t Gross Salary \t Benefits \t Contributions \t Taxes \t Net Salary" << endl;

	while (!fin.eof())	
	{
	cout << name << " \t \t ";
	fout2 << name << " \t \t ";
	
	if (skill < 1 || skill > 4)
	{
		cout << "Bad data. \t Invalid skill level." << endl;
		fout2 << "Bad data. \t Invalid skill level." << endl;
		return 0;
	}
	else if (hours < 0 || hours > 60)
	{
		cout << "Bad data. \t Invalid hours." << endl;
		fout2 << "Bad data. \t Invalid hours." << endl;
		return 0;
	}		
	else if (benefitType < 0 || benefitType > 3)
	{
		cout << "Bad data. \t Invalid benefit code." << endl;
		fout2 << "Bad data. \t Invalid benefit code." << endl;
		return 0;
	}			
	else if (skill == 1 && benefitType != 0)
	{
		cout << "Bad data. \t Invalid benefit type for skill level." << endl;
		fout2 << "Bad data. \t Invalid benefit type for skill level." << endl;
		return 0;
	}		
	else if (skill != 4 && contPercent != 0)
	{
		cout << "Bad data. \t Invalid contribution percent for skill level." << endl;
		fout2 << "Bad data. \t Invalid contribution percent for skill level." << endl;
		return 0;
	}		
	else if (contPercent < 0 || contPercent > 5)
	{
		cout << "Bad data. \t Invalid contribution: " << contPercent << "%" << endl;
		fout2 << "Bad data. \t Invalid contribution: " << contPercent << "%" << endl;
		return 0;
	}			
	
	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);
	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) + (0.07 + (taxRemainder / 100.0)) * (grossDed - 5000);
			
	netSalary = grossDed - taxOwed;

	sumGross += gross;
	sumBenefit += benefitCost;
	sumContributions += contTotal;
	sumTaxes =+ taxOwed;
	sumNet += netSalary;
  
	cout << fixed << setprecision(2) << setw(6);
	cout << "$" << gross << " \t $" << benefitCost << " \t $" << contAmount << " \t $" << taxOwed << " \t $" << netSalary << endl;
	
	fout2 << fixed << setprecision(2) << setw(6);
	fout2 << "$" << gross << " \t $" << benefitCost << " \t $" << contAmount << " \t $" << taxOwed << " \t $" << netSalary << endl;
	
	fin >> skill >> benefitType >> contPercent >> hours;
	getline(fin, taxID);
	getline(fin, name);			
	}   
	
	cout << left << fixed << setprecision(2) << setw(6);
	cout << "Total Gross Salaries paid: \t \t $" << sumGross << endl;
	cout << "Total Benefits collected: \t \t $" << sumBenefit << endl;
	cout << "Total Contributions to Retirement Funds: \t \t $" << sumContributions << endl;
	cout << "Total Taxes collected: \t \t $" << sumTaxes << endl;
	cout << "Total Net Salaries paid: \t \t $" << sumNet << endl;

	fout2 << left << fixed << setprecision(2) << setw(6);
	fout2 << "Total Gross Salaries paid: \t \t $" << sumGross << endl;
	fout2 << "Total Benefits collected: \t \t $" << sumBenefit << endl;
	fout2 << "Total Contributions to Retirement Funds: \t \t $" << sumContributions << endl;
	fout2 << "Total Taxes collected: \t \t $" << sumTaxes << endl;
	fout2 << "Total Net Salaries paid: \t \t $" << sumNet << endl;

	fout2.close();
	}
	
	return 0;
}

what compiler are you using? If it has a debugger then learn to use it so that you can find out what is wrong with your program.

what compiler are you using? If it has a debugger then learn to use it so that you can find out what is wrong with your program.

I've just been using unix. Can you recommend a better one with a debugger (that is free)?

unix is an operating system, not a compiler. You are probably using gcc or g++. The debugger you want is gnu's dbg -- tutorial here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.