9 Replies - 231 Views - Last Post: 11 February 2014 - 09:03 AM Rate Topic: -----

#1 duanuys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 08

Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 07:50 AM

This is the code I am using to take amount of hours worked times wage then that number will be called to another file where if it falls between a certain amount it will return a number corresponding to the amount of federal tax. My problem is that it just multiplies hours and wage and doesn't do what its supposed to do, its hard to explain if you want you can copy and paste and build it to see for yourself. Please help here is the code:


this is main.cpp
#include <iostream>

int nTax(int x);


int main()
{
	using namespace std;

	cout << "Hello, and welcome to the payroll calculator\n\n" << endl;
	cin.get();

	cout << "Please enter your hourly wage." << endl;
	int nWage;
	cin >> nWage;

	cout << "\n\nGreat! Now please enter the amount of hours you have worked thus far" << endl;
	int nHours;
	cin >> nHours;

	//this is your gross income wage*hours
	int nGrossIncome = nWage*nHours;  

	//this is a function call of nTax that calculates federal tax from your gross income
	int nFedDeduct = nTax(nGrossIncome);

	cout << nFedDeduct << endl;


	double dSocialSec;
	dSocialSec = 0.062;

	double dMedicare;
	dMedicare = 0.0145;

	double dFinal = (nGrossIncome - ((nGrossIncome*dSocialSec) + (nGrossIncome*dMedicare) + nFedDeduct));

	cout << "\nIn the state of Texas, this would be your payroll\nfor the following conditions:\n\n";
	cout << "Single , Biweekly, 3 Federal Allowances" << endl;
	cout << "\nYour Final Pay will be: " << dFinal << endl;


}



this is nTax.cpp

#include <iostream>



int nTax(int x)
{	

	if (x > 820 && x < 840)
		int x = 27 + 2;
	if (x > 840 && x < 860)
		int x =27 + 2*2;
	if (x>860 && x < 880)
		int x = 27 + 2*3;              // $820 to 920
	if (x > 880 && x < 900)
		int x = 27 + 2*4;
	if (x>900 && x < 920)
		int x = 27 + 3+(2*4);



	if (x > 920 && x < 940)
		int x = 27 + 3*2+(2*4);
	if (x > 940 && x < 960)
		int x = 27 + 3 * 3 + (2 * 4);
	if (x > 960 && x < 980)
		int x = 27 + 3*4+(2*4);           //$920 to $1020
	if (x > 980 && x < 1000)
		int x = 27 + 3 * 5 + (2 * 4);
	if (x > 1000 && x < 1020)
		int x = 27 + 3 * 6 + (2 * 4);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Please help me with this code, this is a simple payroll calculator

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8909
  • View blog
  • Posts: 33,398
  • Joined: 12-June 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 07:55 AM

Quote

My problem is that it just multiplies hours and wage and doesn't do what its supposed to do, its hard to explain if you want you can copy and paste and build it to see for yourself.

Would you be more specific? Is it giving you the wrong answer? If so what are you inputting, what is it spitting out, and what should it be outputting? Is it not looping or doing some sort of over arching function? If so what is it doing versus what should it be doing.
Was This Post Helpful? 0
  • +
  • -

#3 duanuys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 08:01 AM

View Postmodi123_1, on 11 February 2014 - 07:55 AM, said:

Quote

My problem is that it just multiplies hours and wage and doesn't do what its supposed to do, its hard to explain if you want you can copy and paste and build it to see for yourself.

Would you be more specific? Is it giving you the wrong answer? If so what are you inputting, what is it spitting out, and what should it be outputting? Is it not looping or doing some sort of over arching function? If so what is it doing versus what should it be doing.


sorry its hard to explain I will try to give it my best. In sequential order. When you start the program it asks to enter the amount of hours you worked, then you input that.

Then it asks your hourly wage, the user inputs that

then to get a gross income it multiples wage and hours together.. so far it is doing its job. Now where it doesn't go as planned is at that point it is supposed to take the gross income and run it through another function called 'nTax' where if that number falls inbetween a certain range it is supposed to return a specific number that corresponds to federal tax.

and i test that by the line

int nFedDeduct = nTax(nGrossIncome);

	cout << nFedDeduct << endl;



it is supposed to spit out the number corresponding to federal tax. for example if the gross income falls between 800 and 820 it is supposed to return 27, because that is the federal tax they tax you on for that income range.

Now when it runs all it does when i cout<<nFedDeduct is just give me the product of wage and hours... its like it didn't even go to the function nTax.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8909
  • View blog
  • Posts: 33,398
  • Joined: 12-June 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 08:09 AM

So you need to read up on scope again. Maybe twice.

Take this chunk of code:

05	int nTax(int x)
06	{  
07	 
08	    if (x > 820 && x < 840)
09	        int x = 27 + 2;


You have an incoming variable of X.

Then you decide to recreate X (on line 09) to do some math... and that's it. You do not return a variable.. you do not have the incoming X be 'pass by reference'.. nothing.

As it stands the incoming X parameter is 'pass by value'.. it just shells its value into the method and that's it. You need to specify a return statement, else line 25 (in the main) doesn't fill the nFedDeduct variable.

25	    int nFedDeduct = nTax(nGrossIncome);



The take away is two things..
1. get a return value for that function.
2. stop redefining X inside a different scope level. Use a different variable name.. declare it once, at the top of the function, and return that.
Was This Post Helpful? 0
  • +
  • -

#5 duanuys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 08:14 AM

Also i forgot to add the nTax.cpp goes on for a long while, i only added a snippet of the code.. just to make more sense.. the end of the nTax.cpp after all the code should say return x;

blah blah blah

return x;



Just to be clear, I am redefining X too many times within nTax.cpp

a better code would be the following:


int nTax(int)
{
if (blah blah)
  int y=27+2
  return y;
}

Just to be clear, I am redefining X too many times within nTax.cpp

a better code would be the following:

[code]

int nTax(int)
{
if (blah blah)
  int y=27+2
  return y;
}


Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8909
  • View blog
  • Posts: 33,398
  • Joined: 12-June 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 08:17 AM

Okay.. so that makes it super hard to help you if what you are presenting is not the complete picture. Moving target and all.

As for "I am redefining X too many times" it is less a question of quantity, but of doing it at all. So I guess any time you are doubling up on variables then yes.. that is too many times.
Was This Post Helpful? 0
  • +
  • -

#7 duanuys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 08:20 AM

All I am saying is i don't understand what you mean by

Quote

The take away is two things..
2. stop redefining X inside a different scope level. Use a different variable name.. declare it once, at the top of the function, and return that.


can i ask that you please just give me an example of what I have and what the correct way of doing it is.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8909
  • View blog
  • Posts: 33,398
  • Joined: 12-June 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 08:33 AM

You have a variable X as a parameter.. then, inside said function, you are defining X again and again. Stop doing that. Use a different variable name, and declare it once, at the top.


Quote

can i ask that you please just give me an example of what I have and what the correct way of doing it is.

How about you try first.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 08:50 AM

If you're confused by functions, perhaps reading the tutorials in my signature may be helpful.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,300
  • Joined: 25-December 09

Re: Please help me with this code, this is a simple payroll calculator

Posted 11 February 2014 - 09:03 AM

Perhaps a short tutorial on variable scope might also help?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1