Issue with expected output, always comes out 0

I am doing an assignment for c++ class. I have to say that I am a beginner in every sense of the word, and it is an online class, so the pace is quite quick. I am trying to comprehend everything as fast as I can, but am struggling a bit. The assignment is to do a program that figures tax based on income and some other variables (dependents, etc.). Every time I enter any numbers in at all, and it doesn't matter whether I choose S or M for single or married, the value returned in the end calculation is 0.

Like I said, I literally started programming 3 weeks ago, so I apologize for anything in the code that looks amateur, but I am an amateur at this point. I am not asking for a finished code solution, but if someone could point me in the right direction, I would be extremely grateful!

Thanks!

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

char maritalStatus;
int children;
double salary;
double pension;
double standardExemption;
double personalExemption;
double tax;

double getData();
double taxAmount (double salary, double pension, double standardExemption, double personalExemption);

int main ()
{
    getData ();
    taxAmount (salary, pension, standardExemption, personalExemption);

    cout << "The amount of Federal Tax you owe is: " << tax << endl;


}

double getData ()
{
    char maritalStatus;
    int children;
    double salary;
    double pension;
    double standardExemption;
    double personalExemption;



    cout << "Please enter whether you are [M]arried or [S]ingle: " << endl;
    cin >> maritalStatus;

    switch (maritalStatus)
    {
    case 'M':
    case 'm':
        cout << "Please enter how many children you have under the age of 14: " << endl;
        cin >> children;

        cout << "Please enter the combined gross salary of you and your spouse: " << endl;
        cin >> salary;

        cout << "Please enter the percentage of gross income you contributed to a pension fund: " << endl;
        cin >> pension;

        standardExemption = 7000;
        personalExemption = 3000 + children * 1500;

        return children, salary, pension, standardExemption;
        break;

    case 'S':
    case 's':
        cout << "Please enter your gross salary: " << endl;
        cin >> salary;

        cout << "Please enter the percentage of gross income you contributed to a pension fund: " << endl;
        cin >> pension;

        standardExemption = 4000;
        personalExemption = 1500;

        return salary, pension, standardExemption, personalExemption;
        break;

    default:
        cout << "Invalid choice, please select M for married, or S for single."
            << endl;
    }
}

    double taxAmount (double salary, double pension, double standardExemption, double personalExemption)
{
    
    double taxableIncome;
    double pensionAmount;
    double exemptionAmount;
    double tax;

    pensionAmount = salary * (pension * .01);
    exemptionAmount = standardExemption + personalExemption + pensionAmount;
    taxableIncome = salary - exemptionAmount;

    if (taxableIncome <= 15000)
        tax = taxableIncome * .15;

        if (taxableIncome > 15000 && taxableIncome <= 40000)
            tax = 2500 + (.25 * (taxableIncome - 15000));

            if (taxableIncome > 40000)
                tax = 8460 + (.35 * (taxableIncome - 40000));

    return tax;
    
} 

Check out scope.

Also double getData () says that is going to return one double.
Thank you for the reply ne555. I have not run across the word scope in our textbook yet, so I am not quite sure what you mean by check out scope?

Since you say that double getData () is only going to return one double, I assume it's because the parentheses are empty? Does that mean since I want to return 4 doubles from that function that I should list all of those in the function header or the prototype before main?

Thanks again for the help!
> I am not quite sure what you mean by check out scope?
That you need to look for its definition, in order to understand what's wrong with your code.
http://cplusplus.com/doc/tutorial/variables/

> Since you say that double getData () is only going to return one double, I assume it's because the parentheses are empty?
No. http://cplusplus.com/doc/tutorial/functions/
1
2
3
double //it's going to return a double
getData //name
() //it does not take parameters 


As you need to return several values, you could pass them by reference. http://cplusplus.com/doc/tutorial/functions2/
An alternative is to return an struct that holds those members.
1
2
3
4
5
6
7
8
struct tax{
    double salary;
    double pension;
    double standardExemption;
    double personalExemption;
};

tax getData();
Ok...I've rewritten a good portion of the code, although now it will not compile, and I can't seem to figure out why. I read both the tutorial here and in the textbook from class about reference parameters. I try to run it now and VS says I have error LNK2019: unresolved external symbol "void __cdecl getData(char,int,double &,double &,double &,double &)" .

I'm not sure what that means or how to go about fixing it. I appreciate your help ne555, I am sure this all is trivial to someone of your skill, but I am finding it quite challenging and will feel quite rewarded I am sure when I finally get this working.

Here is my current code:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


void getData(char maritalStatus, int children, double& salary, double& pension, double& standardExemption, double& personalExemption);
double taxAmount (double salary1, double pension1, double standardExemption1, double personalExemption1);

int main ()
{
	double salary1;
	double pension1;
	double standardExemption1;
	double personalExemption1;
	char maritalStatus;
	double children;
	double salary;
	double pension;
	double standardExemption;
	double personalExemption;
	double tax;

    getData (maritalStatus, children, salary, pension, standardExemption, personalExemption);
    taxAmount (salary1, pension1,standardExemption1, personalExemption1);

	cout << "The amount of Federal Tax you owe is: " << tax << endl;
  


}

void getData (char maritalStatus, int children, int& salary, int& pension, int& standardExemption, int& personalExemption)
{
    
    cout << "Please enter whether you are [M]arried or [S]ingle: " << endl;
    cin >> maritalStatus;

    switch (maritalStatus)
    {
    case 'M':
    case 'm':
        cout << "Please enter how many children you have under the age of 14: " << endl;
        cin >> children;

        cout << "Please enter the combined gross salary of you and your spouse: " << endl;
        cin >> salary;

        cout << "Please enter the percentage of gross income you contributed to a pension fund: " << endl;
        cin >> pension;

        standardExemption = 7000;
        personalExemption = 3000 + children * 1500;

        break;

    case 'S':
    case 's':
        cout << "Please enter your gross salary: " << endl;
        cin >> salary;

        cout << "Please enter the percentage of gross income you contributed to a pension fund: " << endl;
        cin >> pension;

        standardExemption = 4000;
        personalExemption = 1500;

        break;

    default:
        cout << "Invalid choice, please select M for married, or S for single."
            << endl;
    }
}

    double taxAmount (double salary1, double pension1, double standardExemption1, double personalExemption1)
{
    double pensionAmount;
	double exemptionAmount;
	double taxableIncome;
	double tax;

    pensionAmount = salary1 * (pension1 * .01);
    exemptionAmount = standardExemption1 + personalExemption1 + pensionAmount;
    taxableIncome = salary1 - exemptionAmount;

    if (taxableIncome <= 15000)
        tax = taxableIncome * .15;

        if (taxableIncome > 15000 && taxableIncome <= 40000)
            tax = 2500 + (.25 * (taxableIncome - 15000));

            if (taxableIncome > 40000)
                tax = 8460 + (.35 * (taxableIncome - 40000));

    return tax;
    
} 
Your function prototype, line 8 is not matching the function, line 34.

int/double.
Thank you Lynx for that. I changed it and it did compile and run! I am now having same issue as I did before though, the final calculation is always 0, which baffles me because this code is now nothing like it was before yet I am yielding the same results...

Any help would be greatly appreciated, as I am completely confused now...

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;


void getData(char maritalStatus, int children, double& salary, double& pension, double& standardExemption, double& personalExemption);
double taxAmount (double salary1, double pension1, double standardExemption1, double personalExemption1);

int main ()
{
	double salary1 = 0;
	double pension1 = 0;
	double standardExemption1 = 0;
	double personalExemption1 = 0;
	char maritalStatus = 0;
	int children = 0;
	double salary = 0;
	double pension = 0;
	double standardExemption = 0;
	double personalExemption = 0;
	double tax = 0;

    getData (maritalStatus, children, salary, pension, standardExemption, personalExemption);
    taxAmount (salary1, pension1,standardExemption1, personalExemption1);

	cout << "The amount of Federal Tax you owe is: " << tax << endl;
  


}

void getData(char maritalStatus, int children, double& salary, double& pension, double& standardExemption, double& personalExemption)
{
    children = 0;
	maritalStatus = 0;
	salary = 0;
	pension = 0;
	standardExemption = 0;
	personalExemption = 0;

    cout << "Please enter whether you are [M]arried or [S]ingle: " << endl;
    cin >> maritalStatus;

    switch (maritalStatus)
    {
    case 'M':
    case 'm':
        cout << "Please enter how many children you have under the age of 14: " << endl;
        cin >> children;

        cout << "Please enter the combined gross salary of you and your spouse: " << endl;
        cin >> salary;

        cout << "Please enter the percentage of gross income you contributed to a pension fund: " << endl;
        cin >> pension;

        standardExemption = 7000;
        personalExemption = 3000 + children * 1500;

        break;

    case 'S':
    case 's':
        cout << "Please enter your gross salary: " << endl;
        cin >> salary;

        cout << "Please enter the percentage of gross income you contributed to a pension fund: " << endl;
        cin >> pension;

        standardExemption = 4000;
        personalExemption = 1500;

        break;

    default:
        cout << "Invalid choice, please select M for married, or S for single."
            << endl;
    }
}

    double taxAmount (double salary1, double pension1, double standardExemption1, double personalExemption1)
{
    double pensionAmount = 0;
	double exemptionAmount = 0;
	double taxableIncome = 0;
	double tax = 0;

    pensionAmount = salary1 * (pension1 * .01);
    exemptionAmount = standardExemption1 + personalExemption1 + pensionAmount;
    taxableIncome = salary1 - exemptionAmount;

    if (taxableIncome <= 15000)
        tax = taxableIncome * .15;

        if (taxableIncome > 15000 && taxableIncome <= 40000)
            tax = 2500 + (.25 * (taxableIncome - 15000));

            if (taxableIncome > 40000)
                tax = 8460 + (.35 * (taxableIncome - 40000));

    return tax;
    
} 
Next to the code tag button is the program output button. Consider copying the program executation data from the console into a replay and putting the program output tags around it. Additionally, please tell us the results from your debugging effort. What have you learned by using the debugger?

Step through main with the debugger. Let us know the value of the tax variable at the point where you are trying to output it to the console. Perhaps while debugging you will see the obvious answer as to why the tax variable is still zero.
Last edited on
Perhaps you'll want to make an adjustment to line 26 so that you are actually doing something with the value taxAmount returns.
1
2
getData (maritalStatus, children, salary, pension, standardExemption, personalExemption);
taxAmount (salary1, pension1,standardExemption1, personalExemption1);


You get the data in variable `a', but use `z'
You don't catch the return value.
Topic archived. No new replies allowed.