Program is not align properly?

Hi, I am having trouble getting my code to align properly in columns. When i enter a three digit number it outputs correctly but when i type in a number greater than 1000

The output looks like this right now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Please enter the sale amount before tax. $78000

        Gross Tax       Discount        Tax Due
--------------------------------------------------
State   $3120.00                $80.50          $3039.50
--------------------------------------------------
City    $2340.00                $61.00          $2279.00
--------------------------------------------------
County  $1170.00                $31.75          $1138.25
--------------------------------------------------
                                 Total: $6456.75

--------------------------------
Process exited after 5.906 seconds with return value 0
Press any key to continue . . .



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
  #include <iostream>
#include <cmath>
#include <iomanip>


using namespace std;


double getSaleAmount();
void calculateTax(double taxes[], const double saleAmount, int taxType);
double computeTaxDue(const double taxRate, const double saleAmount, double &discount);
double computeDiscount(const double grossTax);
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];

	calculateTax(state, saleAmount,0);
	calculateTax(city, saleAmount,1);
	calculateTax(county, saleAmount,2);

	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 calculateTax(double taxes[], const double saleAmount, int taxType)
{
	taxes[2] = computeTaxDue(taxRates[taxType], saleAmount, taxes[1]);
	taxes[0] = taxes[2]+taxes[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;

	double grossTax = taxAmount;

	discount = computeDiscount(grossTax);

	return taxAmount - discount;
}

//----------------------------------------------------------------------------
double computeDiscount(const double grossTax)
{

	double discount;

	if (grossTax <= 100)
	{
		discount = grossTax*.05;
	}
	else if (grossTax > 100)
	{
		discount = (grossTax - 100)*.025 + 5;
	}
	return discount;
}

//----------------------------------------------------------------------------
void drawLine(const char symbol, const unsigned int width)
{
	for (unsigned int x = 0; x < width; x++)
	{
		cout << symbol;
	}
}

Last edited on
I think what you're looking for is setw() from iomanip(see reference: http://www.cplusplus.com/reference/) .

For instance,
cout << "\n\tGross Tax\tDiscount\tTax Due\n";

to
cout << "\n"<<setw(15)<<"Gross Tax"<<setw(15)<<Discount<<setw(15)<<Tax Due\n";
I hope this helps. :)
Topic archived. No new replies allowed.