I want to get the workers salary, then display the salary after taxes are taken out(salary divided by 25%). So I need to know the correct way to divide salary by 25% then make it into a variable called net. Thanks alot.

net = salary / 25%

Printable View

- 09-25-2003robshirahNewbie Question
I want to get the workers salary, then display the salary after taxes are taken out(salary divided by 25%). So I need to know the correct way to divide salary by 25% then make it into a variable called net. Thanks alot.

net = salary / 25% - 09-25-2003axon
25percent = 0.25 so you need to make all your variables double...

Code:`double net = 0;`

double salary = 0;

double tax = .25;

//set salary and other things that your program does

/*[edit]:ooops!*/net = ( salary*tax );

- 09-25-2003Glirk Dient
net = slaray*.25;

- 09-25-2003robshirah
Thanks alot

- 09-25-2003DougDbug
int salary, net; // defines two integer variables

You don't want to "divide by 25%" ! You need to*multiply*by 0.25 to get the tax.**tax = salary * 0.25;**Then, you can subtract this from the salary.

The cleanest way to do this would be to multiply the salary by 0.75 to get the net directly.

It would also be "better" (more flexable) to make the tax rate a variable: float TaxRate = 0.25 ; Then, calculate net = Salary * (1-TaxRate) ;

If you want to show dollars and cents, you'll need to use float rather than int.

I hope that gets you started. If you have problems, post your code. And, if you haven't done so yet, read the board guidelines. They will tell you to use a clear-specific title for your posts, and to post whatever code you have so far. Also see the post regarding code tags. This will tell you*how*to post your code. - 09-25-2003robshirah
I did as you suggested, net = salary/tax; , with salary input at 100 it gives me an answer of 400????? For some reason it seems to be multiplying (salary)100 times 25

double salary; //workers hourly wage

double net; //workers wage after taxes are taken out

double tax; //ammount of taxes to be subtracted from salary

tax = (.25);

cout <<"Please input workers salary: ";

cin >>salary;

cout <<"\n";

net = salary/tax;

cout <<"Here is workers salary after taxes: "<<net;

return 0; - 09-25-2003robshirah
Dougdbug you nailed it, thanks alot, & thanks to everyone for the help.

- 09-25-2003DougDbugGlad I could help !
Computer problems are alot like math word-problems.

There are two problems to solve when writing a program. First, you have to figure out how to solve the problem (the formula or algorithm). Then, you have to figure out how to make the program do it for you.

In this case it would have been helpful to try-out the formula with a calculator first... I should have sugggested that. If you have a slightly more complicated formula, a spreadsheet might be a better way to try it out.

When things get really complicated, or really long (with looping etc.), it's easier to go ahead and write the program and try-out your formula/algorithm in the program. - 09-25-2003robshirah
I have another problem here, I added hours & overtime into the program, overtime pay being salary+1, if over 40 hours. it compiles correctly but it takes the last dollar & rounds up, 41*11 = 451, 451 * .75 = 338.25. When I run the program it comes up with 339 as the gross after taxes answer?!?!

double salary; //workers hourly wage

double gross;

double tax;

double hours;

cout <<"Please enter workers total hours: ";

cin >>hours;

cout <<"\n";

cout <<"Please input workers salary: ";

cin >>salary;

cout <<"\n";

gross = salary*hours;

if (hours <= 40)

tax = gross*.75;

else

gross = (salary+1) * hours;

tax = (gross+1) *.75;

cout <<"Workers gross pay is: "<<gross<<endl;

cout <<"Here is workers salary after taxes: "<<tax;

return 0; - 09-25-2003bennyandthejets
Use code tags please. You've already been told once.

- 09-25-2003bennyandthejets
Your problem occurs because you haven't placed braces around your over 40 hour code:

Code:`if (hours <= 40)`

tax = gross*.75;

else

gross = (salary+1) * hours;

tax = (gross+1) *.75;

Code:`if (hours <= 40)`

tax = gross*.75;

else

{

gross = (salary+1) * hours;

tax = (gross+1) *.75;

}

- 09-25-2003bennyandthejets
Can I ask why you calculate >40 hours tax based on (gross+1)? Shouldn't the tax always be just 75% of gross earnings? I would do it like this:

Code:`cout <<"Please enter workers total hours: ";`

cin >>hours;

cout <<"\n";

cout <<"Please input workers salary: ";

cin >>salary;

cout <<"\n";

if (hours <= 40)

gross = salary*hours;

else

gross = (salary+1) * hours;

tax = gross*.75;

- 09-25-2003axon
robshira: I suggest always using braces...even when you are only executing a single line/item. It is a good hait to get into at an early stage. It saves debugging time when your programming assignemnts get LARGE! so for the if else always use braces:

Code:`if ( condition ) {`

//stuff......

} else if ( condition2 ) {

//more stuff.....

} else { //default

//some more....

}

- 09-26-2003robshirah
Ok, first of all id like to thank all those that helped : ) Now, I still have the same problem, the code is adding 40+ hours(overtime pay) to be more than it acually is.

Code:`#include <iostream.h>`

main()

{

double salary; //workers hourly wage

double gross;

double tax;

double hours;

cout <<"Please enter workers total hours: ";

cin >>hours;

cout <<"\n";

cout <<"Please input workers salary: ";

cin >>salary;

cout <<"\n";

gross = salary*hours;

if (hours <= 40)

tax = gross*.75;

else

{

gross = (salary+1) * hours;

tax = (gross+1) *.75;

}

cout <<"Workers gross pay is: "<<gross<<endl;

cout <<"Here is workers salary after taxes: "<<tax;

return 0;

}

- 09-26-2003eladCode:
`if (hours <= 40)`

tax = gross*.75;

else

{

gross = (salary+1) * hours;

tax = (gross+1) *.75;

}

review the above like you have posted vs below

if (hours <= 40)

{

net = gross*.75;

}

else

{

gross = (salary+1) * hours;

net = gross *.75;

}