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
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%
25percent = 0.25 so you need to make all your variables double...
axonCode:double net = 0;
double salary = 0;
double tax = .25;
//set salary and other things that your program does
/*[edit]:ooops!*/net = ( salary*tax );
net = slaray*.25;
Thanks alot
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.
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;
Dougdbug you nailed it, thanks alot, & thanks to everyone for the 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.
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;
Use code tags please. You've already been told once.
Your problem occurs because you haven't placed braces around your over 40 hour code:
This code means that your after tax salary is always calculated based on (gross+1). Change it to this:Code:if (hours <= 40)
tax = gross*.75;
else
gross = (salary+1) * hours;
tax = (gross+1) *.75;
Then it works fine.Code:if (hours <= 40)
tax = gross*.75;
else
{
gross = (salary+1) * hours;
tax = (gross+1) *.75;
}
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:
Besides being more correct (IMO), that code is actually faster (technically) and more efficient.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;
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....
}
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;
}
Code: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;
}
That shouldn't make a difference elad.
robshirah, why didn't you take my advice? I'm pretty sure your formulae are wrong. These should be the calculations:
<40 Hours:
gross=salary*hours
tax=gross*.75
>40 Hours:
gross=(salary+1)*hours
tax=gross*.75
Using those formulae should fix any problems you're having. Plus, using my structure will make your program more efficient.
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;
OK once again thanks to all who helped, I finally got it :D, here it is.
Code:#include <iostream.h>
main()
{
double salary;
double hours;
double tax;
double gross;
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;
cout <<"The workers salary after taxes is: " <<tax;
return 0;
}
Although your code works you should really use the new headers and declare a return type for main (which would be int):
Code:#include <iostream>
using namespace std;
int main()
{
...
A couple of minor points.
C++ style is to declare variables at or just before the point of first use, rather than all at the start of a function or block.
Also, your program source code is a form of communication between you and any readers of the code - including yourself when you come back to it later. For that reason you need to consider whether what you've written makes sense. Although this is a trivial program, if you read through it you'll find that it includes amongst it's variables one named salary and one named tax. There is an output statement that says "The workers salary after tax is " but then, it doesn't output salary at all, it outputs tax. That suggests that you haven't named your variables in the most logical way. This is even more obvious when you consider that the value held by the variable tax isn't the amount of tax, it's the net salary. The value held by salary is the hourly rate.
Getting your program to work is one thing, but it's important also that it makes sense to someone reading it.