Reputation: 105
I'm really new to C++, and this is homework. I don't understand what's going on. When I run this, everything works fine if I put in 60000 for current salary and .05 for pay increase, but if I put in something like 52000 for current salary and .23 for pay increase, then the retroactive pay comes out as 5982.00 instead of 5980.00. Is it something to do with the decimals or something? I don't really know. Thanks in advance.
// Variables
char fullName[30]; // The user's full name - INPUT
int currentAnnual; // The users's current annual salary - INPUT
float percentIncrease; // The percent increase due on the
// current annual salary - INPUT
// The retroactive pay
float retroactive;
// The new monthly salary based on the pay increase and the new salary
float monthlySalary;
// The new salary the user should receive based on their old salary
// and their pay increase
float newSalary;
for(int lcv = 1; lcv <= 3; lcv++)
{
// INPUT
cout << "What is your full name? ";
cin.getline(fullName, 30);
cout << "What is your current salary? ";
cin >> currentAnnual;
cout << "What is your pay increase (please input percentage in"
"decimal format)? ";
cin >> percentIncrease;
// PROCESSING
newSalary = (currentAnnual * percentIncrease) + currentAnnual;
monthlySalary = newSalary / 12;
retroactive = (monthlySalary - (currentAnnual / 12)) * 6;
// OUTPUT
cout << endl;
cout << fullName << "'s Salary Information\n";
cout << left << setw(15) << "New Salary" << setw(19) << "Monthly Salary"
<< "Retroactive Pay\n";
cout << setprecision(2);
cout << fixed;
cout << right << setw(10) << newSalary << setw(19) << monthlySalary
<< setw(20) << retroactive << endl << endl;
cout << "<Press enter to continue>";
cin.ignore(100, '\n');
cin.ignore(1000, '\n');
cout << endl;
}
Upvotes: 1
Views: 2340
Reputation: 10665
Change currentAnnual / 12
to currentAnnual / 12.0
to force a floating-point calculation to be done. Otherwise, that part of the computation will be rounded off to the nearest integer below.
Upvotes: 2
Reputation: 75926
currentAnnual / 12
The division of two integers in C is an "integer division" (it gives an integer), and I think you dont want that. One solution is to change it to currentAnnual / 12.0
. Anyway, it's important that you understand what is happening here.
Upvotes: 2
Reputation: 7115
Floating point calculations are not as accurate as you might expect. Try replacing float
with double
in the above code (double
is a "double-precision floating-point number"; it can store many more values than a float
, and is much more accurate).
Also, order of operations is sometimes significant, so you may want to re-order the retroactive
calculation to something like
6 * monthlySalery - currentAnnual / 0.5;
and try other combinations to see what works best.
Upvotes: 0
Reputation: 5338
You should do monthlySalary = newSalary / 12.0;
instead of monthlySalary = newSalary / 12;
so you have to specify that you want to divide on float number. Otherwise result will be integer. For example 125./12 = 10
but 125./12. = 10.41(6)
. And of course you get wrong results.
Try to add .0
or just .
to all your constants.
Upvotes: 0