9 Replies - 1350 Views - Last Post: 07 November 2006 - 03:04 PM Rate Topic: -----

#1 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Functions and parameter passing help

Posted 06 November 2006 - 06:53 PM

Hi everyone. I'm a n00bie both to the site and to C++ programming.

I've been working on this programming assignment this past week and I'm just stumped.
Probably something simple and stupid that I missed since I tend to do that and this
past week was rediculously busy (taking 5 classes this semester)

Anyway, here's the contents of the "payroll.txt" file accessed in the following program:

Quote

John Smith#10.45 40 15
Jane Doe#12.50 45 15
Harry Morgan#20.00 40 20
Carmen Martinez#25.00 35 25
Jacintha Washington#50.85 60 35


And here's what I have as far as code goes. I'm sure my math for the processPay function is wrong.
And I'm also sure something else is screwed up since it only seems to output 1 line.

Help!

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

void reportDescription();
void reportTitle();
void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax);
void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossTotal, double& netTotal);
void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net);
void totalAmounts(double totalGross, double totalNet);

int main()
{	
	string empName;
	double payRate;
	double hoursWorked;
	double taxRate;
	double grossPay;
	double netPay;
	double grossPayTotal;
	double netPayTotal;

	ifstream inFileData;
	inFileData.open("c:\\payroll.txt");

	reportDescription();
	reportTitle();

	while (!inFileData.eof())
	{

		inputData(inFileData, empName, payRate, hoursWorked, taxRate);
		
	}
	inFileData.close();

	processPay(payRate, hoursWorked, taxRate, grossPay, netPay, grossPayTotal, netPayTotal);
	printEmployeeInfo(empName, payRate, hoursWorked, taxRate, grossPay, netPay);
	totalAmounts(grossPayTotal, netPayTotal);

	return 0;
}

void reportDescription()
{
	cout << "This program calculates paychecks for each employee. "<< endl
		 << "A text file will be created outside of the program with the information: "<< endl
		 << "name, rate of pay, number of hours worked, and tax percentage to be deducted." << endl
		 << endl
		 << "The program will create a report in columnar format showing the employee "<< endl
		 << "name, hourly rate, number of hours worked, tax rate, gross pay, and net pay. "<< endl
		 << endl
		 << "After all employees are processed, totals will be displayed total gross amount "<< endl
		 << "and total net pay." << endl
		 << endl << endl;
}

void reportTitle()
{
	cout << setfill(' ') << right << setw(38) << "Payroll Report" << endl;
}

void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax)
{
	getline(inFileData, name, '#');
	inFileData >> pay >> time >> tax;
	inFileData.ignore(100, '\n');

}

void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossPayTotal, double& netPayTotal)
{
	double temptax = netPay * tax;
	
	netPay = payrate * hours;
	grossPay = netPay - temptax;

	grossPayTotal = grossPayTotal + grossPay;
	netPayTotal = netPayTotal + netPay;

}

void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net)
{
	cout << fixed << showpoint << setprecision(2);  

	cout << left << setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"  << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
	cout << left << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked" << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
	cout << endl;

	cout << left << setw(20) << name << setw(10) << setw(10) << pay << setw(10) << time << setw(10) << tax << setw(10) << gross << setw(10) << net << endl;

}

void totalAmounts(double totalGross, double totalNet)
{

	cout << left << setw(5) << "Totals" << right << setw(10) << totalGross << setw(10) << totalNet << endl;

}


Is This A Good Question/Topic? 0
  • +

Replies To: Functions and parameter passing help

#2 NyeNye  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06

Re: Functions and parameter passing help

Posted 06 November 2006 - 07:03 PM

is their any errors....please if their are?
Was This Post Helpful? 0
  • +
  • -

#3 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Re: Functions and parameter passing help

Posted 06 November 2006 - 07:34 PM

It compiles fine, with no errors. But the output isn't what it should be.

Here's a pic of what the output looks like:

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 NyeNye  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06

Re: Functions and parameter passing help

Posted 06 November 2006 - 07:37 PM

Oh,..i can't see your output from my pc...OMG

damn pc...

hmm..it's maybe some logic error or something..hmm

let me see..
Was This Post Helpful? 0
  • +
  • -

#5 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Re: Functions and parameter passing help

Posted 06 November 2006 - 07:54 PM

I copied the text from the command line output. Let me see if it'll post as it looks on the screen:

This program calculates paychecks for each employee.
A text file will be created outside of the program with the information:
name, rate of pay, number of hours worked, and tax percentage to be deducted.

The program will create a report in columnar format showing the employee
name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.

After all employees are processed, totals will be displayed total gross amount
and total net pay.


						Payroll Report
Employee			Hourly	Hours	 Tax	   Gross	 Net
Name				Rate	  Worked	Rate	  Amount	Amount

					50.85	 60.00	 35.00	 323958709722612380000000000000
0000000000000000000000000000000000.003051.00
Totals3147027465876806800000000000000000000000000000000000000000000000.00-925596
31349317830000000000000000000000000000000000000000000000.00
Press any key to continue . . .




That's basically what it looks like. It doesn't print any of the employee names and only looks like
it prints the last line of data from the file. Also, my math is obviously messed up somewhere. lol
Was This Post Helpful? 0
  • +
  • -

#6 NyeNye  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06

Re: Functions and parameter passing help

Posted 06 November 2006 - 08:03 PM


............

 double payRate;
	double hoursWorked;
	double taxRate;
	double grossPay;
	double netPay;
	double grossPayTotal;
	double netPayTotal;
..



some of the declaration could be on int rather on double..
Was This Post Helpful? 0
  • +
  • -

#7 jayhuang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 11-October 06

Re: Functions and parameter passing help

Posted 06 November 2006 - 09:10 PM

View Postrichiewinters, on 6 Nov, 2006 - 07:34 PM, said:

It compiles fine, with no errors. But the output isn't what it should be.

Here's a pic of what the output looks like:

Posted Image



try to limit the decimal places of the outputs. There are three lines of commands for that. can't remember them now.
Was This Post Helpful? 0
  • +
  • -

#8 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Re: Functions and parameter passing help

Posted 06 November 2006 - 09:22 PM

I already had that set up in one of the functions, but I added it to the other one that I thought could use it just now.

Didn't seem to help.

I think I have a looping problem somewhere (at least as part of the problem) because it seems like only the last line of the payroll.txt file prints to the screen.
Was This Post Helpful? 0
  • +
  • -

#9 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Re: Functions and parameter passing help

Posted 07 November 2006 - 11:27 AM

Figured out the looping problems and all that, but still need help with the math in the processPay function.
Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Functions and parameter passing help

Posted 07 November 2006 - 03:04 PM

View Postrichiewinters, on 7 Nov, 2006 - 06:27 PM, said:

Figured out the looping problems and all that, but still need help with the math in the processPay function.

Should the output be?
This program calculates paychecks for each employee. 
A text file will be created outside of the program with the information: 
name, rate of pay, number of hours worked, and tax percentage to be deducted.

The program will create a report in columnar format showing the employee 
name, hourly rate, number of hours worked, tax rate, gross pay, and net pay. 

After all employees are processed, totals will be displayed total gross amount 
and total net pay.


						Payroll Report
Employee			Hourly	Hours	 Tax	   Gross	 Net	   
Name				Rate	  Worked	Rate	  Amount	Amount	

John Smith		  10.45	 40.00	 15.00	 355.30	418.00	
Jane Doe			12.50	 45.00	 15.00	 478.13	562.50	
Harry Morgan		20.00	 40.00	 20.00	 640.00	800.00	
Carmen Martinez	 25.00	 35.00	 25.00	 656.25	875.00	
Jacintha Washington 50.85	 60.00	 35.00	 1983.15   3051.00   
Totals   4112.83   5706.50





program is now
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void reportDescription();
void reportTitle();
void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax);
void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossTotal, double& netTotal);
void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net);
void totalAmounts(double totalGross, double totalNet);

int main()
{	
	string empName;
	double payRate;
	double hoursWorked;
	double taxRate;
	double grossPay;
	double netPay;
	double grossPayTotal=0;				  // ** zero these totals
	double netPayTotal=0;

	ifstream inFileData;
	inFileData.open("payroll.txt");

	reportDescription();
	reportTitle();

	while (!inFileData.eof())
	{

		inputData(inFileData, empName, payRate, hoursWorked, taxRate);
		// ** moved following lines
	processPay(payRate, hoursWorked, taxRate, grossPay, netPay, grossPayTotal, netPayTotal);
	printEmployeeInfo(empName, payRate, hoursWorked, taxRate, grossPay, netPay);
		
	}
	inFileData.close();

	totalAmounts(grossPayTotal, netPayTotal);

	return 0;
}

void reportDescription()
{
	cout << "This program calculates paychecks for each employee. "<< endl
		 << "A text file will be created outside of the program with the information: "<< endl
		 << "name, rate of pay, number of hours worked, and tax percentage to be deducted." << endl
		 << endl
		 << "The program will create a report in columnar format showing the employee "<< endl
		 << "name, hourly rate, number of hours worked, tax rate, gross pay, and net pay. "<< endl
		 << endl
		 << "After all employees are processed, totals will be displayed total gross amount "<< endl
		 << "and total net pay." << endl
		 << endl << endl;
}

void reportTitle()
{
	cout << setfill(' ') << right << setw(38) << "Payroll Report" << endl;
}

void inputData(ifstream& inFileData, string& name, double& pay, double& time, double& tax)
{
	getline(inFileData, name, '#');
	inFileData >> pay >> time >> tax;
	inFileData.ignore(100, '\n');
//cout << " " <<  pay << " " << time << " " <<  tax;
}

void processPay(double payrate, double hours, double tax, double& grossPay, double& netPay, double& grossPayTotal, double& netPayTotal)
{
	
	netPay = payrate * hours;
	double temptax = netPay * tax/100.0;		// ** move this!!
	grossPay = netPay - temptax;

	grossPayTotal = grossPayTotal + grossPay;
	netPayTotal = netPayTotal + netPay;

}

void printEmployeeInfo(string name, double pay, double time, double tax, double gross, double net)
{
	 static int first=1;
	cout << fixed << showpoint << setprecision(2);  
   if(first)	  // ** only print title first time in
	 {
	  cout << left << setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"  << setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
	  cout << left << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked" << setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
	  cout << endl;
	  first=0;
	  }
	cout << left << setw(20) << name << setw(10) << setw(10) << pay << setw(10) << time << setw(10) << tax << setw(10) << gross << setw(10) << net << endl;

}

void totalAmounts(double totalGross, double totalNet)
{

	cout << left << setw(5) << "Totals" << right << setw(10) << totalGross << setw(10) << totalNet << endl;

}


Remember when declaring a local variable (inside {}) such as
double grossPayTotal=0; // ** zero these totals
double netPayTotal=0;
its value is by default undetermined; make such you initialise such variables.

This post has been edited by horace: 07 November 2006 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1