Hi. Beginner (obviously) here with a problem that's got me stumped. I'm supposed to create a function that takes the total income entered by the user, takes 10 percent of it, and displays the value. However, when I run the program, it doesn't do it correctly.
This is the first part of the code, the function that multiplies by .10 to get 10 percent of the total income.
pass by reference is something you build into the function is the easiest way to describe it.
So if the only thing you want the function to do is update the income in main
1 2 3 4
void computePercentage(double &income){
income += income*.10; // or income = 1.1*income whichever you like
return ;
}
and in main computePercentage(income);
The result if you print it out in main is income will have changed. cout << income;
PS
'income' seems to me to be a confusing name for that variable. In ordinary language the percentage income derived from the principal is:
income = percentageEarned = principal * interest
I'd look at it this way. In the original function, the multiplication is performed, but nothing is done with that result, it is simply discarded.
1 2 3 4
double computePercentage(double income){
(income * .10); // calculation is done and then thrown away.
return income;
}
A simple fix would be use a local variable to store that result, then return it, like this:
1 2 3 4
double computePercentage(double income){
double result = (income * .10); // calculation is done and stored.
return result; // return the stored value
}
Or more simply:
1 2 3
double computePercentage(double income){
return (income * .10); // calculation is done and the answer is returned.
}
The result is in fact returned as 'income' from the function as in line 8. That 'income' is used to set the value of budgetPercentage as the statement reads. OP is working on the misunderstanding of scope in that 'income' in the function is the same 'income' as the one in main. OP has made the mistake of getting confused by using the same names ( and misleading ones on top of that ). Obvious to all of us except OP perhaps. :)
The variable income isn't actually created in main. It's a variable I create at the beginning, and I don't even call it in main. I just create the functions that utilize that variable.
After putting in both of your suggestions, it didn't change the result. It still came out wrong. Instead of printing 100 when you put in 1000 for income, it only prints 1000.
I both changed it to pass by reference and I also changed it to make it return the result directly, but neither one worked.
I guess that's why I don't understand why I would have to pass by reference. The only variable income exists outside of main, created above all of the functions, and it is only called through the function display like I've shown above. Therefore, I can't have 2 variables, just the one that I created above.
I'm still stumped. I can't get it to print anything but 1000 for the variable budgetPercentage when I put in 1000 for income.
yeah, that's a good point when you say income is not created in main. I should have said 'in display()'
I suggest you change the variable name 'income' in all lines of computePercentage() to 'anIncome' or even better just 'x' because, with all due respect, I reckon your problem is due primarily to bad naming. The rest follows. Let us know how you get on. :)
I found it. It turns out I was actually working on the project and not the .cpp file, therefore it wasn't building a new solution and running the same thing every time.
The solution that Chervil posted was what fixed the problem. Thanks for the help!
Yeah, chervil was right. I missed the point he/she was making in the first place because I misread the line (income * .10); which doesn't do anything other than multiply two numbers and then lose the result.