cout.precision

Here's my code so far:

float total(float subTotal)
{
cout.precision(4);
float taxes;
float grandTotal;
float tax;

tax = .06;
taxes = subTotal * tax;
grandTotal = taxes + subTotal;
cout << "Taxes: $" << taxes << endl;
cout << endl;
cout << "Your grand total is: $" << grandTotal << endl;

return (grandTotal);
}

My problem is that sometimes the tax displays as something like 0.12 and then sometimes it displays as something like 0.7223.

Can anyone help me?

i think it has to do something with type you are using, meaning the FLOAT
I'd use DOUBLE instead to secure against sudden truncation...

btw, this may help too
http://www.cplusplus.com/reference/iostream/ios_base/precision/

and speaking about your cout.precision(4)... Lets say your subTotal = 2, you will always get taxes = 0.12 in result, check the link above for it
1
2
3

cout.precision(4);


I'm rather new myself but as far as I know you have set the code to display the next 4 digits meaning it will display 0.0000 but if you put
1
2
3

cout.precision(2);


that will only show the next 2 digits e.g 0.00

Hope this helps but remember im new myself

also at the top where you have written
1
2

float total(float subTotal)


I would put something like
 
float total, subTotal;
Last edited on
sorry for trashing, this will be kinda offtop post...
@DragonKnight - I'm quite newb too and i don't feel any shame about it
Even if we both are damn wrong about the solution NOONE will insult you here, and that's great about this forum - only serious people here...
Why i wrote this? well... I have a comparision to other c++ forums (especially my native ones)
It's just that I have had many a bad experience on other c++ forums, some people in this world seem to get such a kick out of putting others down.

Thanks though John, as your right this site has been nothing but welcoming so far!! :-)
Thank you all very much for your help.
Topic archived. No new replies allowed.