1

I'm writing a simple program in C++ to calculate the tax for a purchase and output it. My code is outputting the text and the variables that are set and not changed. But instead of outputting the variables that are being calculated and set based on the tax rates it just outputs 0. Thanks for any help! First week of class for me so try to dumb it down if you can :)

Here's the code:

#include <iostream>
using namespace std;

int main()
{
int price = 52, stateTaxRate = 4, countyTaxRate = 2;

double stateTax = price / 100 * stateTaxRate;
double countyTax = price / 100 * countyTaxRate;
double totalTax = countyTax + stateTax;
double total = price + totalTax;

cout << "State Tax: " << stateTax << " ";

cout << "County Tax: " << countyTax << " ";

cout << "Total Tax: " << totalTax << " ";

cout << "Total: " << total << " ";  
}
8
  • 7
    52 / 100 is 0. that's why all the rest is 0 Commented Aug 29, 2012 at 20:04
  • 4
    You're integer-dividing. 52 / 100 equals 0...cast to a double or better yet, just declare everything as doubles. Commented Aug 29, 2012 at 20:04
  • 1
    Change: int price = 52, stateTaxRate = 4, countyTaxRate = 2; to double price = 52, stateTaxRate = 4, countyTaxRate = 2; integer division price/100 will return 0. Commented Aug 29, 2012 at 20:05
  • 1
    Just look at the number of answers that are the same but no one is willing to up vote anyone...my human beings are odd creatures, heaven forbid someone who has answered up votes someone else's answer. Commented Aug 29, 2012 at 20:06
  • 4
    Everybody wrote basically at the same time, it's just that. Commented Aug 29, 2012 at 20:11

9 Answers 9

6

You're doing integer division.

 int price = 52, stateTaxRate = 4, countyTaxRate = 2;

Is making the evaluation of

 price / 100

result in 0.

An integer divided by another integer won't result in a floating point number in C++. It instead truncates (ie removes) everything you'd expect to see after the ".". It doesn't know at the inner most point of the expression you're evaluating that eventually everything will be turned to a double.

Change your variables to doubles, and you'll have a different result.

Sign up to request clarification or add additional context in comments.

Comments

2

Your inputs are integers and the equations are set up in such a way that the result will be "treated" as an integer too. For example (simplified to get the point across):

int price = 52;
double stateTax = price / 100;

stateTax will be 0 because 52/100 = 0.52 as an int, which is truncated to 0.

Make price a double in the calculation to get the expected result:

int price = 52;
double stateTax = (double)price / 100;

Because 52.0/100 = 0.52 as a double.

Comments

2
double stateTax = price / 100 * stateTaxRate;

price / 100 where price is int is 0. Use

double stateTax = static_cast<double>(price) / 100 * stateTaxRate;

And in other cases too.

3 Comments

Or make the integer variable double as well.
You are quicker i delete mine :)
multiply on 0.01 or divide on 100.0 easier =)
2

price / 100 when price = 52 is 0. If you change price to double you will get what you want.

Comments

2

Typecasting problem. Declare like this

double price = 52, stateTaxRate = 4, countyTaxRate = 2;

Comments

1

Change the types of your variable from int to double.

Integer variables make the compiler use integer operators, and the integer division operator (/) truncates the results. That's your problem.

(price / 100) * tax or price / (100 * tax) (whichever the compiler wants to evaluate first) will both return 0.

Comments

1

You're expecting fractional - floating-point - results from your calculations, but they're all using integral values, so you'll lose the fractional parts.

For instance in this calculation: double stateTax = price / 100 * stateTaxRate;, price/100 will end up as zero. Since this is integer division, the fractional part is truncated.

You need to force floating-point calculations. Here's one way (note that it uses 100.0 rather than 100):

double stateTax = price / 100.0 * stateTaxRate;

Or, since price is at the root of the calculations, declare it as a double:

double price = 52.0;

Comments

1

Simply change 100 to 100.0 To us it is the same but with that extra .0 C++ will know that it is double and you want double result.

Why? When calculating C++ takes type which is more precise.

int + int = int;
int + double = double;
short int + int = int;

100 = int
100u = unsigned
100.0 = double
100.0f = float

Comments

1

int / int = int. Even if you're storing the result in a double, the result gets truncated before it ever makes it into the double.

double stateTax = price / 100 * stateTaxRate;
is the same as:
double = int / int * int, which means the result will be an int and then stored in the double.

Try writing 100 as 100.0. Or you can just make your ints into doubles or floats.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.