Using a pointer in functions

Hi, I'm working on an assignment that wants me to calculate Personal Income Tax. I'm being limited to using pointers instead of passing by reference. I know that in the real world you would not rely on pointers this much but this is simply an exercise.

The tax is calculated as:
10% on taxable income from $0 to $8925 plus
15% on taxable income from $8925 to $36250 plus
25% on taxable income from $36250 to $73200 plus
28% on taxable income from $73200 to $111525 plus
33% on taxable income from $111525 to $199175 plus
35% on taxable income from $199175 to $225000 plus
39.6% on taxable income over $225000

I'm having difficulty implementing a pointer in my CalcTaxes function. I want to be able to pass the value of the counter to the other functions that rely on a counter. I've been working on trying to make my code clean and efficient but I feel all I've ended up with is a rather ugly brute force solution. I would appreciate any critique and any help towards finding a solution to my problem.

Thanks in advance!

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 #include <iostream>
#include <iomanip>
#include <string>

#define personalExempt 3900
#define standardDeduct 6100
#define toPercent 100
using namespace std;

const double TAX_RATES[] = {0.1,0.15,0.25,0.28,0.33,0.35,0.396};
const double TAX_BRACKET[] = {0, 8925, 36250, 73200, 111525, 199175, 225000};

enum{TIER1, TIER2, TIER3, TIER4, TIER5, TIER6, TIER7};

struct Income
{
	string name;
	double AGI;
	double taxableIncome;
	double taxes[7];
	double totalTaxes;
	double disposable;
};

void ReadInfo (Income* info);
void CalcTaxes(double agi, Income* taxPtr);
int TaxPartition(double taxedIncome, Income* taxPart);
void TaxBreakdown(Income* taxPtr, int count);
double TaxDue(Income* taxPtr, int count);
void TaxReport(Income* report, int count);
void PrintLine(int size, char ch);

int main()
{
	Income user;
	
	ReadInfo(&user);
	CalcTaxes(user.AGI, &user);

	return 0;
}

void ReadInfo (Income* info)
{
	bool valid;

	cout << "Name: ";
	getline(cin, info->name);
	do
	{
		cout << "Adjusted Gross Income: ";
		cin >> info->AGI;
		if (cin.fail() || info->AGI <= 0)
		{
			cin.clear();
			valid = false;
			cout << "Error: That was not a valid input\n";
		}
		else
		{
			valid = true;
		}
		cin.ignore(1000,'\n');
	}while (!valid);
}

void CalcTaxes(double agi, Income* taxPtr)
{	
	double taxedIncome; 
	int count;
	
	taxPtr->taxableIncome = agi - personalExempt - standardDeduct;
	taxedIncome = taxPtr->taxableIncome;
	count = TaxPartition(taxedIncome, taxPtr);
	TaxBreakdown(taxPtr, count);
	TaxDue(taxPtr, count);
	taxPtr->disposable = taxPtr->taxableIncome - taxPtr->totalTaxes;
}


int TaxPartition(double taxedIncome, Income* taxPart)
{
	int i = 0;
	bool more = true;
	int count = 0;
	do
	{
		if (taxPart->taxableIncome > TAX_BRACKET[i+1])
		{
			count++;
		}
		else if (taxPart->taxableIncome > TAX_BRACKET[6])
		{
			count = 6;
			more = false;
		}
		else
		{
			more = false;
		}
		i++;
	}while(more);
	
	return count;
}

void TaxBreakdown(Income* taxPtr, int count)
{
	switch (count)
	{
	case TIER1:
		if (taxPtr->taxableIncome <= 0)
		{
			cout << "Tax Exemptions exceed taxable income!\n";
		}
		else 
			taxPtr->taxes[count] = (taxPtr->taxableIncome - TAX_BRACKET[count]) * TAX_RATES[count];
		break;
	case TIER2:
	case TIER3:
	case TIER4:
	case TIER5:
	case TIER6:
	case TIER7:
		for (int i = 0; i < count; i++)
		{
			taxPtr->taxes[i] = (TAX_BRACKET[i+1] - TAX_BRACKET[i]) * TAX_RATES[i];
		}
		taxPtr->taxes[count] = (taxPtr->taxableIncome - TAX_BRACKET[count]) *TAX_RATES[count]; 
		break;
	}
}

double TaxDue(Income* taxPtr, int count)
{
	taxPtr->totalTaxes = 0;
	for(int i = 0; i <= count; i++)
	{
		taxPtr->totalTaxes += taxPtr->taxes[i]; 
	}
	return taxPtr->totalTaxes;
}

void TaxReport(Income* report, int count)
{

}

void PrintLine(int size, char ch)
{
	cout << setw(size) << setfill(ch)
		 << "" << setfill(' ') << endl;
}
Last edited on
looks like you are passing a struct to some of you functions, but are also passing specific data members of that struct to the function as well. You should just pass the struct.
Last edited on
Thanks I'll be sure to do that before I turn this in. :) By the way, If I have a pointer of type int that I want to use to pass count to the other functions would I declare that in my CalcTaxes function and then pass to the other functions in CalcTaxes? Sorry if it sounds obvious I just want to make sure I'm doing this right.
Topic archived. No new replies allowed.