The program is written to calculate a new salary after a 5% raise or 10% raise.
However, the program does not calculate the new salary after a 10% raise correctly. Salary of $10000, the new salary should be $11000 with a 10% raise. The program mistakenly gives $550 more than that. Any pointers to correct the program so it will calculate new salary after 5% and 10% raise correctly?
#include <cstdlib>
#include <iostream>
usingnamespace std;
void fivePercentRaise(double &);
void tenPercentRaise(double &);
int main()
{
double salary = 0.0;
cout << "Please enter current salary: ";
cin >> salary;
fivePercentRaise(salary);
tenPercentRaise(salary);
system("pause");
return 0;
}
void fivePercentRaise(double & sal)
{
sal = sal + sal * 0.05;
cout << "New salary if you receive a 5% raise: " << sal << endl;
}
void tenPercentRaise(double & sal)
{
sal = sal + sal * 0.10;
cout << "New salary if you receive a 10% raise: " << sal << endl;
}
You use a reference when you intend to modify the original argument fed to the function. You apparently don't want to modify the original or you'd be happy with the result you're getting.
In otherwords, fivePercentRaise modifies salary in main. You feed that modified value to tenPercentRaise.