Calcuate Sales Tax Program Trouble

Hi, I wrote a program and my instructor wants me to condense the calculateState, calculateCity, and calculateCounty to one function. Since my program runs how it is right now, is there a way to calculate all this without in one function without repeating code.

Also, my instructor requires and wants me to change the function named double computeDiscount to pass only a double grossTax.

Here is the code I have now, can you help me with fixing it the way he wants. I have been trying to figure a way to redo it, but have not been able to find a way yet.

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
155
156
157
158
159
160
161
162
163
164
  #include <iostream>
#include <cmath>

using namespace std;

void calculateState(double state[], const double saleAmount);
void calculateCounty(double county[], const double saleAmount);
void calculateCity(double city[], const double saleAmount);
double getSaleAmount();
double computeTaxDue(const double taxRate, const double saleAmount, double &discount);
double computeDiscount(const double saleAmount, double discount, double taxAmount);
void drawTable(double city[], double state[], double county[]);
void drawLine(const char symbol, const unsigned int width);

const double taxRates[3] = { .04, .03, .015 };


const unsigned int width = 35;
const char symbol = '-';

int main()

{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	const double saleAmount = getSaleAmount();

	double state[3];
	double city[3];
	double county[3];

	calculateState(state, saleAmount);
	calculateCity(city, saleAmount);
	calculateCounty(county, saleAmount);

	drawTable(city, state, county);


	return 0;

}

//----------------------------------------------------------------------------
void drawTable(double city[], double state[], double county[])
{
	cout << "\n\n";
	drawLine('-', 50);
	cout << "\n\tGross Tax\tDiscount\tTax Due\n";

	drawLine('-', 50);
	cout << "\nState\t";

	for (int i = 0; i < 3; i++)
	{
		cout << "$" << state[i] << "\t\t";
	}
	cout << endl;


	drawLine('-', 50);
	cout << "\nCity\t";

	for (int i = 0; i < 3; i++)
	{
		cout << "$" << city[i] << "\t\t";
	}
	cout << endl;

	drawLine('-', 50);
	cout << "\nCounty\t";

	for (int i = 0; i < 3; i++)
	{
		cout << "$" << county[i] << "\t\t";
	}
	cout << endl;

	drawLine('-', 50);
	cout << "\n\t\t\t\t Total:\t" << "$" << county[2] + state[2] + city[2];
	cout << endl;
}
//----------------------------------------------------------------------------
void calculateState(double state[], const double saleAmount)
{
	state[0] = taxRates[0] * saleAmount;
	state[1] = computeDiscount(saleAmount, 0, state[0]);
	state[2] = (state[0]) - (state[1]);
}
//----------------------------------------------------------------------------
void calculateCity(double city[], const double saleAmount)
{
	city[0] = taxRates[1] * saleAmount;
	city[1] = computeDiscount(saleAmount, 0, city[0]);
	city[2] = (city[0]) - (city[1]);
}
//----------------------------------------------------------------------------
void calculateCounty(double county[], const double saleAmount)
{
	county[0] = taxRates[2] * saleAmount;
	county[1] = computeDiscount(saleAmount, 0, county[0]);
	county[2] = (county[0]) - (county[1]);
}


//----------------------------------------------------------------------------
double getSaleAmount()
{

	double saleAmount = 0.00;

	do
	{
		cout << endl;
		cout << "Please enter the sale amount before tax. " << "$";
		cin >> saleAmount;

		if (saleAmount <= 0)
		{
			cout << endl;
			cout << " Sorry! You have entered an invalid sale amount. " << endl;
			cout << " Please try again." << endl;
			cout << endl;
		}
	} while (saleAmount <= 0);

	return saleAmount;

}

//----------------------------------------------------------------------------
double computeTaxDue(const double taxRate, const double saleAmount, double &discount)
{
	double taxAmount = taxRate*saleAmount;

	discount = computeDiscount(saleAmount, discount, taxAmount);

	return taxAmount - discount;
}

//----------------------------------------------------------------------------
double computeDiscount(const double saleAmount, double discount, double taxAmount)// double computeDiscount(double grossTax)
{
	if (taxAmount <= 100)
	{
		discount = taxAmount*.05;
	}
	else if (taxAmount > 100)
	{
		discount = (taxAmount - 100)*.025 + 5;
	}
	return discount;
}

//----------------------------------------------------------------------------
void drawLine(const char symbol, const unsigned int width)
{
	for (unsigned int x = 0; x < width; x++)
	{
		cout << symbol;
	}
}
The three routines are identical except for the name of the first argument (which irrelevant) and the index into the taxRate table. Pass the tax rate in as an additional argument.

1
2
3
4
5
void calculateTax (double rslts[], const double saleAmount, double rate)
{   rslts[0] = rate * saleAmount;
    rslts[1] = computeDiscount (saleAmount, 0, rslts[0]);
    rslts[2] = (rslts[0]) - (rslts[1]);
}


Topic archived. No new replies allowed.