Alignment Problems

Hi, my program is not working for numbers greater than three digits. Like if the user enters 100, the alignment is correct, but if the user enters 78000, the alignment is off by a tab? Is there a way to make this work?

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
#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('-', 65);
	cout << "\n\tGross Tax\tDiscount\tTax Due\n";

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

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


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

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

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

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

	cout << endl;

	drawLine('-', 65);
	cout << "\n\t\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;
	}
}

Try using setw before outputting your numbers or text ( http://www.cplusplus.com/reference/iomanip/setw/ )

You can also use setfill if you want to use a different character for the padding ( http://www.cplusplus.com/reference/iomanip/setfill/ )
Last edited on
Can you demonstrate how to do that because I couldn't get it to work.
This just tweaks the example that's in the page Norm Gunderson linked.

1
2
3
4
5
6
7
8
#include <iostream>    
#include <iomanip> 

int main () {
    std::cout << std::left << std::setw(14) << "Name" << std::setw(20) << "Course" << std::setw(6) << "Year" << std::setw(3) << "Grade"  << std::endl;
    std::cout << std::left << std::setw(14) << "John Doe" << std::setw(20) << "Intro to Chemistry" << std::setw(6) << "2015" << std::setw(3) << "88"  << std::endl;
    return 0;
}

Last edited on
I think the example that rjvc gave you should work:

From http://www.cplusplus.com/forum/beginner/147095/ :

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";


You'll probably have to change the 15 to however many characters you want each title & number to take up, but the above code looks sound to me.
Last edited on
Topic archived. No new replies allowed.