Need help with Decimals!!

Sorry to bother everyone but, I've only been at C++ since Wednesday. I need to know how to multiply decimals. Been looking for a FAQ or something but maybe I did not look hard enough.

This is my code

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
string name;
int grosspay;
int federalIncomeTax = .15;
double stateTaxRate = 0.35;
double socialSecurityTax = 0.575;
double medicareTaxRate = 0.275;
double pensionPlan = 0.500;
double healthInsurance = 075.50;
double incomeTax = grosspay * federalIncomeTax;

cout << "Enter Employee Name \n";
getline (cin, name);
cout << "Enter Payment \n";
cin >> grosspay;

cout << incomeTax;
cout << fixed << showpoint << setprecision(2);








return 0;
}

It looks pretty bad right now I know. I've been emailing friends but they keep giving different answers. The last part of this is just the calculations. If I can get grosspay * federalIncomeTax to show the proper decimal instead of a huge
number or 0 I'll be fine. If anyone can tell me what is wrong I'll be grateful. Thanks.
int federalIncomeTax = .15;

What kind of integer is 0.15 ? Doesn't look like an integer to me.

I see that your code calculates incomeTax using the value grosspay before grosspay has been given a value. If you want better results, you will have to give grosspay the right value before you use it.
Last edited on
It isn't. Like I said I'm going off of what people have been telling me. At first a friend told me to make it a double so it could have decimal places. After that I changed it to something, and then something else again. This is the final mess we're looking at. What should it be?
It should be something that isn't restricted to an integer. A double or a float would do.

You principal problem is that your logic is all out of order. You are doing this:

1) Calculate values from user input that hasn't even been entered yet.
2) Get user input
3) Show answers

You cannot make calculations now based on data that you will collect in the future. Try:

1) Get user input
2) Calculate values from user input
3) Show answers
Yeah, I know that lol. It WAS (key word being was) much neater. I'm aware that that I need to have things in that order. But when I got to the calculations, I always got 0 or another whole number. If I can figure out how to multiply the grosspay (which is a decimal) by the other values (which is are decimals)and not get zeroes I'd be okay.
Whenever you use a double where C++ expects an int this will happen:
1
2
3
4
int n = .5; //.5 gets cast to (turned into) an int, so n is now 0 (the integer part of .5)
double x = .4; //fine
cout << n * x << '\n'; //it prints 0 because n contains 0 and x contains .4, (0 * .4 == 0)
//instead make n a double 

Now re-look over your code...
Dude... THANK YOU. Like I said, I've only been at it since Wednesday. I tried this on a much smaller program I made and it worked! If I turn those others into doubles it should be cool, right?
Oh and also do I include the set precision before or after I get the output of the answer? I need 2 decimal places.
You should set precision before you calculate the answer.
Topic archived. No new replies allowed.