Multiplication isn't working correctly

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.

1
2
3
4
double computePercentage(double income){
	(income * .10);
	return income;
}


It should, if I remember right, return the income value after the calculation has happened, correct?

Here's where this program is implemented:

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
double display(double income, double budgetliving, double livingexpenses, double taxeswithheld, double percentage, double otherexpenses) {
	double budgettax;
	double budgetPercentage;
	double budgetother;
	double actualdifference;
	double budgetdifference;
	budgettax=computeTax(income);
	budgetPercentage=computePercentage(income);
	budgetother=income-budgettax-budgetPercentage-budgetliving;
	actualdifference = income - taxeswithheld - percentage - livingexpenses - otherexpenses;
	budgetdifference = 0;
	
	cout << "Item" << setw(24) << "Budget" << setw(16) << "Actual" << endl;
		cout << "=============== =============== ===============" << endl;
		cout << fixed;
		cout << setprecision(2);
		cout << "Income: " << setw(9) << "$" << setw(14) << income << setw(2) << "$" << setw(14) << income << endl;
		cout << "Taxes: " << setw(10) << "$" << setw(14) << budgettax << setw(2) << "$" << setw(14) << taxeswithheld << endl;
		cout << "Percentage: " << setw(8) << "$" << setw(14) << budgetPercentage << setw(2) << "$" << setw(14) << percentage << endl;
		cout << "Living: " << setw(9) << "$" << setw(14) << budgetliving << setw(2) << "$" << setw(14) << livingexpenses << endl;
		cout << "Other: " << setw(10) << "$" << setw(14) << budgetother << setw(2) << "$" << setw(14) << otherexpenses << endl;
		cout << "=============== =============== ===============" << endl;
		cout << "Difference: " << setw(5) << "$" << setw(14) << budgetdifference << setw(2) << "$" << setw(14) << actualdifference << endl;
		return 0;
}


I can't find out why it won't correctly multiply the numbers and then post the correct number. When I put in 1000 for the income, it outputs 120.

What's happening, and how do I fix it?

Thanks for the help.
Last edited on
closed account (48T7M4Gy)
The 'income' you are returning from computePercentage is not the same 'income' you passed in the function, it is a copy which you modify and return.

If you want the income in main to change you need to pass by reference ie &income.

Or in main : income += computePercentage(income);


Where do I have to pass by reference? In the function I create, or in where I call it, or both?
closed account (48T7M4Gy)
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


Last edited on
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.
}
closed account (48T7M4Gy)
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. :)
Last edited on
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.
closed account (48T7M4Gy)
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!
closed account (48T7M4Gy)
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.
Topic archived. No new replies allowed.