10 Replies - 1214 Views - Last Post: 10 October 2009 - 10:52 PM Rate Topic: -----

#1 swolll  Icon User is offline

  • New D.I.C Head

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

Code Help - First Time Using Input/Output Stream

Posted 08 October 2009 - 10:45 PM

I've written the code, but it doesn't compile right. The user is supposed to name an input and an output file, and then repeat that one more time. But, after the 1st output file is input, the program stops compiling.

Any ideas with the code:
#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, 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;
	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;
		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; 
	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.): " << endl;	
	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;
		getline(fin1, taxID);
		getline(fin1, name);

		cout << "Enter output file name for company payroll report: ";	
		getline(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 (!fin1.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;
	
	fin1 >> skill >> benefitType >> contPercent >> hours;
	getline(fin1, taxID);
	getline(fin1, 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;
}



Thanks so much.

Is This A Good Question/Topic? 0
  • +

Replies To: Code Help - First Time Using Input/Output Stream

#2 swolll  Icon User is offline

  • New D.I.C Head

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

Re: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 12:26 AM

Bumpp.
Was This Post Helpful? 0
  • +
  • -

#3 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 06:56 AM

Line 48: warning: converting to 'int' from 'double'
Line 50: warning: converting to 'int' from 'double'
Line 52: warning: converting to 'int' from 'double'
Line 54: warning: converting to 'int' from 'double'
Line 63: warning: suggest parentheses around assignment used as truth value
Line 75: warning: converting to 'int' from 'double'
Line 216: warning: converting to 'int' from 'double'
Line 218: warning: converting to 'int' from 'double'
Line 220: warning: converting to 'int' from 'double'
Line 222: warning: converting to 'int' from 'double'
Line 231: warning: suggest parentheses around assignment used as truth value
Line 243: warning: converting to 'int' from 'double'



Line 47 to 54:
  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;



skillPay is an Integer data type. You are trying to assign a floating point (decimal) value to it. Thats not possible.

Line 63:
if (skill = 1 || benefitType == 0)



When you use the "=" sign you are saying "Make whats on the left equal whats on the right".
When you use the "==" operator you are asking "does whats on the left equal whats on the right?"

refer to: http://www.cprogramm...al/lesson1.html

You have also tried to compare allot of other integer to floating points.
Refer to this for help: http://www.learncpp....-point-numbers/

Also please refer here for help on C++ condition operators: http://www.cprogramm...al/lesson2.html


Good luck :)

This post has been edited by Delta_Echo: 09 October 2009 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 swolll  Icon User is offline

  • New D.I.C Head

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

Re: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 11:24 AM

Thanks so much. A few more questions...
1) I think I messed up with the input/output file streams, as the program stops halfway through compilation. Any ideas?

2) The input file I was given has information in lines like:
"0 14 4 100 40 123123123123 John Smith"

When I do getline(fin, taxID) and getline(fin, name) it picks up that whole line with the numbers before. How can I pick up just the taxID and just the name? I was trying cin.ignore to no avail.
Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 11:53 AM

What is the taxID?

is it 123123123123

or
0 14 4 100 40 123123123123

Edit:

Here is one solution to get the taxID and then the name:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	string line = "0 14 4 100 40 123123123123 John Smith";

	stringstream ss;

	ss.str(line);

	int t;

	ss >> t >> t >> t >> t >> t;
	
	string taxId;

	ss >> taxId;

	string name;
	getline(ss, name);

	cout << "TaxID: " << taxId << endl;

	cout << "Name: " << name << endl;

	return 0;
}

This post has been edited by eker676: 09 October 2009 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 swolll  Icon User is offline

  • New D.I.C Head

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

Re: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 01:59 PM

Thanks. Is there a simpler way to do it with cin.ignore or fin.ignore, though?
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: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 02:12 PM

Delta_Echo: Did you find those on your own, or is there a debugger I could be using to help me out? Free?
Was This Post Helpful? 0
  • +
  • -

#8 swolll  Icon User is offline

  • New D.I.C Head

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

Re: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 02:19 PM

eker. taxID is the one long number. right now i'm doing:
		fin >> skill >> benefitType >> contPercent >> hours;
		getline(fin, taxID);	
		getline(fin, name);



And then it gets the rights fin's, but then reads the whole line for both get lines...
Was This Post Helpful? 0
  • +
  • -

#9 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Code Help - First Time Using Input/Output Stream

Posted 09 October 2009 - 02:35 PM

What Operating system are you using? Google search an application called Code::Blocks.
It has ALLOT of tools for C++. Its free, and very powerful. It contains a debugger, compiler and a highlighted text assisted edit.
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: Code Help - First Time Using Input/Output Stream

Posted 10 October 2009 - 05:12 PM

View PostDelta_Echo, on 9 Oct, 2009 - 01:35 PM, said:

What Operating system are you using? Google search an application called Code::Blocks.
It has ALLOT of tools for C++. Its free, and very powerful. It contains a debugger, compiler and a highlighted text assisted edit.

Thanks. Any ideas for the two above questions?
Was This Post Helpful? 0
  • +
  • -

#11 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Code Help - First Time Using Input/Output Stream

Posted 10 October 2009 - 10:52 PM

You may find this extremely useful. Its a website that has a fully detailed reference for the C++ language:http://www.cplusplus.com/reference/
This particular link will be the most useful: http://www.cplusplus...istream/ignore/


The question is what do you want to do with the ignore() function?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1