Variable values going crazy between function calls

I have a tax calculation program that uses multiple function calls within a main(), one function in particular calling several calculation functions within the function (exerpt calling those functions below).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//this function exerpt is named 'readdata()'
if (files.is_open()) {
                outputheader(files, outfile);
				do {
					files >> lastname >> marital >> children >> income >> pension;
					errordataM = errordata_m(files, marital, errordataM);
					errordataP = errordata_p(files, errordataP, pension);
                    exemptions = exemptionfunc(files, marital, children, exemptions);
                    taxincome = taxincomefunc(files, income, pension, deductions, taxincome, exemptions);
                    taxowed = taxowedfunc(files, taxincome, taxowed);
					}
					outputdata(files, outfile, errorfile, filename, lastname, errormessageM, errormessageP, marital, children,
						errordataM, errordataP, income, pension, deductions, taxincome, taxowed, exemptions);
				} 	while (files);
			}


It uses a do-while loop to process data from an input file:
1
2
3
4
5
6
7
Smith S 0 35000.00 .02
Jones M 2 60000.00 .05
Harvey M 0 100000.00 .04
Potter Q 1 55000.00 .03
Groober S 1 75000.00 .0
Chacha M 1 150000.00 .03
Deluca S 1 70000.00 .50

and then make calculations with that data.

It's supposed to output something like
1
2
3
4
5
6
7
8
========================================================================
Name     Gross-Income Exemptions Pension  Taxable-Income Tax-Owed: 
========================================================================
Smith    35000.00     3700.00    2.00 %   30600.00       9900.00   
Jones    60000.00     8800.00    5.00 %   48200.00       14300.00  
Harvey   100000.00    7400.00    4.00 %   88600.00       39470.00  
Groober  75000.00     4400.00    0.00 %   70600.00       33170.00  
Chacha   150000.00    8100.00    3.00 %   137400.00      56550.00

with "error-causing" lines output to a separate error file.

Except I was noticing the calculations yielding very odd output numbers, and ran some tests by embedding cout statements into the calculation functions and figured out that it is the "exemptions" variable that is messing up---and it's primarily (but not exclusively) messing up after one of the error lines that are being output to a separate file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
float exemptionfunc(ifstream& files, char marital, int children, float exemptions) {
	switch (marital) {
		case 'M': {
				exemptions = 6000 + (700 * 2) + (700 * children);
				cout << "M " << exemptions << "\n";
				break;
		}
		case 'S': {
				exemptions = 3000 + 700 + (700 * children);
				cout << "S " << exemptions << "\n";
				break;
		}
		default: {
				exemptions = 700 + (700 * children);
				cout << "O " << exemptions << "\n";
				break;
		}
	}
	return exemptions;

Somewhere between this function (which is outputting the correct exemptions value based on my test cout statements) and this function
1
2
3
4
5
6
float taxincomefunc(ifstream& files, float income, float pension, float deductions, float exemptions, float taxincome) {
	deductions = pension * income;
	taxincome = (income - exemptions) - deductions;
	cout << "(" << income << " - " << exemptions << ") - " << deductions << "\n";
	return taxincome;
}

which is NOT yielding the correct value whatsoever, the value for "exemptions" is getting seriously distorted.
Last edited on
Please don't start a new thread for the same problem.

Please see my last response in your previous thread:
http://www.cplusplus.com/forum/beginner/193873/#msg932525
I may have figured it out. If I call exemptionfunc() from the taxincomefunc() instead of from readdata(), it it uses the correct values for the calculations and therefor outputs the correct "taxincome" values AND THEN outputs incorrect data for "exemptions" (because taxincomefunc() cannot return both "exemptions" and "taxincome")---and if I call exemptionfunc() again inreaddata() AFTER the taxincomefunc() has been calculated, it displays both the correct value for exemptions and the correct calculations.

It works, but it seems excessive, as if I'm only superficially solving the symptoms rather than the problem. Does anyone know why I have to directly call exemptionfunc() within taxincomefunc() for it to yield the correct value, and why the value for "exemptions" gets distorted when called by readdata()?
Last edited on
I apologize for the new thread, but even though this was posted for the same project, it appeared to be a completely different issue at hand. I can delete this if need be.
First your using too many parameters in your function calls. Look at this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
float exemptionfunc(ifstream& files, char marital, int children, float exemptions) {
	switch (marital) {
		case 'M': {
				exemptions = 6000 + (700 * 2) + (700 * children);
				cout << "M " << exemptions << "\n";
				break;
		}
		case 'S': {
				exemptions = 3000 + 700 + (700 * children);
				cout << "S " << exemptions << "\n";
				break;
		}
		default: {
				exemptions = 700 + (700 * children);
				cout << "O " << exemptions << "\n";
				break;
		}
	}
	return exemptions;

The only variables this function needs is children and marital. The variable exemptions should be local to this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
float exemptionfunc(char marital, int children) 
{
    float exemptions = 0.0;
	switch (marital) {
		case 'M': 
			exemptions = 6000 + (700 * 2) + (700 * children);
			cout << "M " << exemptions << "\n";
			break;	
		case 'S': 
			exemptions = 3000 + 700 + (700 * children);
			cout << "S " << exemptions << "\n";
			break;
		default: 
			exemptions = 700 + (700 * children);
			cout << "O " << exemptions << "\n";
			break;
	}

	return exemptions;
}


The same here too many parameters are confusing the issues:

1
2
3
4
5
6
float taxincomefunc(ifstream& files, float income, float pension, float deductions, float exemptions, float taxincome) {
	deductions = pension * income;
	taxincome = (income - exemptions) - deductions;
	cout << "(" << income << " - " << exemptions << ") - " << deductions << "\n";
	return taxincome;
}

You don't use the ifstream so don't pass it. And remember you're passing everything by value. Is that really what you want?

Next you're passing deductions and taxincome into the function when you really should have these variables local to the function.

This is a very simplified version of that function.
1
2
3
4
float taxincomefunc( float income, float pension, float exemptions) 
{
	return((income - exemptions) - (pension * income));
}

By the way are you sure your calculation is correct?

Second I second the "Please don't start a new thread for the same problem. " statement.


Last edited on
Topic archived. No new replies allowed.