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 << " ";  
}
Blake
  • 756
  • 3
  • 16
  • 34
  • 7
    52 / 100 is 0. that's why all the rest is 0 – Andrey Chernukha Aug 29 '12 at 20:04
  • 4
    You're integer-dividing. `52 / 100` equals 0...cast to a double or better yet, just declare everything as doubles. – lc. Aug 29 '12 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. – artapet Aug 29 '12 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. – JonH Aug 29 '12 at 20:06
  • 4
    Everybody wrote basically at the same time, it's just that. – Spidey Aug 29 '12 at 20:11
  • Seeing as how it's your first week, you might not have seen [this question](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c), but try to prefer using `std::cout` etc. instead of `using namespace std;`. – chris Aug 29 '12 at 20:31
  • @chris: While there are good reasons never to put a using-directive in a header, and to avoid it in a wide scope, it's quite reasonable to use one to remove clutter from a small piece of example code. – Mike Seymour Aug 29 '12 at 22:03
  • @MikeSeymour, But being in the first week, how much do you want to bet it's not just there in example code? – chris Aug 29 '12 at 22:41

9 Answers9

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.

bames53
  • 86,085
  • 15
  • 179
  • 244
Doug T.
  • 64,223
  • 27
  • 138
  • 202
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.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
2

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

Grzegorz
  • 3,207
  • 3
  • 20
  • 43
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.

TaZ
  • 743
  • 7
  • 15
2

Typecasting problem. Declare like this

double price = 52, stateTaxRate = 4, countyTaxRate = 2;
SandBag_1996
  • 1,570
  • 3
  • 20
  • 50
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;
pb2q
  • 58,613
  • 19
  • 146
  • 147
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.

Spidey
  • 2,508
  • 2
  • 28
  • 38
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.

Piotr Sobiegraj
  • 1,775
  • 16
  • 26
derpface
  • 1,611
  • 1
  • 10
  • 20
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
Krzychu8
  • 33
  • 6