Blake
Blake

Reputation: 762

Error using cout with variables in C++

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 << " ";  
}

Upvotes: 1

Views: 505

Answers (9)

derpface
derpface

Reputation: 1691

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.

Upvotes: 1

Krzychu8
Krzychu8

Reputation: 33

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

Upvotes: 1

Doug T.
Doug T.

Reputation: 65649

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.

Upvotes: 6

pb2q
pb2q

Reputation: 59667

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;

Upvotes: 1

Spidey
Spidey

Reputation: 2589

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.

Upvotes: 1

SandBag_1996
SandBag_1996

Reputation: 1601

Typecasting problem. Declare like this

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

Upvotes: 2

TaZ
TaZ

Reputation: 743

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.

Upvotes: 2

Grzegorz
Grzegorz

Reputation: 3355

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

Upvotes: 2

ForEveR
ForEveR

Reputation: 55897

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.

Upvotes: 2

Related Questions