USING PASS-BY-REFERENCE TO SEND TWO VALUES TO THE CALLING FUNCTION

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib> 
#include <iostream> 
using namespace 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; 
} 
Last edited on
Don't use references.
what do you mean by that?
I mean: "Don't use references."

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.
thank you, I was able to figure it out! you saved my night.
Topic archived. No new replies allowed.