0

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    The assignment operator returns a value. You do the assignment within the cast expression, and then the assignment returns the same value which is then cast to double and unused. Didn't you read the error message? – aslg Sep 20 '15 at 21:41
  • You'd have to *assign* the *result* of the `static_cast` to some variable, just as the compiler says. (In the same way you have to write `int x = f(5);` and not `f(int x = 5);`.) But a `double` is a very bad data type if you require exactly two decimal places to begin with, so this is probably an XY problem and the best solution to your problem will be something entirely different. – 5gon12eder Sep 20 '15 at 21:43
  • In reply to your deleted comment: Please *treat* warnings as errors (use the `-Werror` flag). While you start learning programming, the compiler is probably right and you should take its warnings serious. That said, even experienced programmers are well-advised to write warning-free code. I always compile with `-Wall -Wextra -Werror -pedantic`. Sloppiness does not pay off. – 5gon12eder Sep 20 '15 at 21:47
  • Do you actually know what static_cast does? – user253751 Sep 20 '15 at 21:53
  • This question doesn't relate to `static_cast` at all, but formatting textual representation of `double`. – πάντα ῥεῖ Sep 20 '15 at 22:04
  • @5gon12eder thanks a lot for this advice. I will try to follow it closely. – LearningCoding Sep 20 '15 at 22:08

3 Answers3

2
  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
Community
  • 1
  • 1
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
1
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.

2785528
  • 5,438
  • 2
  • 18
  • 20
0

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;
Maksim Solovjov
  • 3,147
  • 18
  • 28