LearningCoding
LearningCoding

Reputation: 63

Static cast not working as intended in C++

So this is my code:

static_cast<double>(income_Tax = incomeTax*GrossAmount);
static_cast<double>(provincial_Tax = provincialTax*GrossAmount);
static_cast<double>(social_Security_Tax= socialSecurityTax*GrossAmount);
static_cast<double>(medicaid_Tax = medicare*GrossAmount);
static_cast<double>(pension_Plan = pensionPlan*GrossAmount);
static_cast<double>(health_Insurance = healthInsurance);
static_cast<double>(netPay = GrossAmount - (income_Tax + provincial_Tax + social_Security_Tax + medicaid_Tax + pension_Plan + health_Insurance));

What I am trying to achieve with this static_cast here is: I want the values income_Tax, netPay, provincial_Tax, etc. to have 2 decimal points, regardless of whether they are integers or not. But this is not happening.

Instead, I get 7 warnings (1 for each static cast) like this:

paychecks.cpp:66:5: warning: expression result unused [-Wunused-value]
static_cast<double>(netPay = GrossAmount - (income_Tax + provincial_Tax + social_Security_Tax + medicaid_Tax + pension_Plan + health_Insurance));

And the end result is something like this:

Gross Amount: ............$5000
Federal Income Tax: ......$750
Provincial Tax: ..........$175.4
Social Security Tax: .....$287.5
etc. 

How do I make it so that the end values (e.g. netPay) to be 2 decimal points, regardless of anything?

Upvotes: 0

Views: 2862

Answers (3)

2785528
2785528

Reputation: 5566

static_cast<double>(income_Tax = incomeTax*GrossAmount);

I suggest you fix these one at a time ... an example -- for this first one, I think you mean ...

income_tax = static_cast<double>(incomeTax*GrossAmount);

If both incomeTax and GrossAmount are already doubles, you do not need the static cast.

income_tax = incomeTax*GrossAmount;

The concept of fixed point display (i.e. 2 decimal points) is an output modifier. SO has plenty of examples.

Continue with the other errors.

Upvotes: 1

Maksim Solovjov
Maksim Solovjov

Reputation: 3157

The code that you've written (static_cast<double>(income_Tax = incomeTax*GrossAmount);) is equivalent to:

income_Tax = incomeTax*GrossAmount;
double tmp = income_Tax;
static_cast<double> tmp;

As you can see, your cast does not apply to income_Tax. You wanted to write:

income_Tax = static_cast<double>(incomeTax*GrossAmount);

However, if the type of income_Tax is double, a static_cast will be applied implicitly, so you don't have to do it.

As to having two decimal spaces, you cannot enforce it with a double. The number will have as much precision as it deems necessary. What you can do is always display the number with two digits precision:

std::cout << std::fixed << std::setprecision(2) << income_Tax;

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22214

  1. Unless you do a static_cast<void> you want to store the result of static_cast somewhere. e.g. double income_Tax = static_cast<double>(incomeTax*GrossAmount); Read about static_cast.
  2. You don't really need a cast of any sort to convert an int to double. Just double d = incomeTax*GrossAmount;
  3. A double will normally be printed with more that 2 decimal points. You may want to have a look at this answer

Upvotes: 2

Related Questions