Need help formatting this table? It's almost perfect...

"Electric sander" is the longest string and it makes the table print off center.

Here is a picture of the table now:
https://i.gyazo.com/63285171300f83cb2325b963de6be8e6.png

I want it to look like this:

(Original table)
Part Number Part Description Quantity Price
----------------------------------------------------------------
83 Electric sander 7 $57.98
24 Power saw 18 $99.99
7 Sledge hammer 11 $21.50
77 Hammer 76 $11.99
39 Lawn mower 3 $79.50
68 Screwdriver 106 $6.99
56 Jig saw 21 $11.00
3 Wrench 34 $7.50

Sorted by Description in ascending order
----------------------------------------------------------------
83 Electric sander 7 $57.98
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
68 Screwdriver 106 $6.99
7 Sledge hammer 11 $21.50
3 Wrench 34 $7.50

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
// U14.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include <iomanip>
#include "Invoice.h"

using namespace std;

void selectionSort(Invoice invoices[], const int NUM_ITEMS);
void swapObject(Invoice &a, Invoice &b);
void insertion_sort(Invoice invoices[], const int NUM_ITEMS);
void getCost(Invoice *invoices, const int NUM_ITEMS);

int main()
{
	const int NUM_ITEMS = 8;
	Invoice invoices[] =
	{
		Invoice(83, "Electric sander", 7, 57.98),
		Invoice(24, "Power saw", 18, 99.99),
		Invoice(7, "Sledge hammer", 11, 21.5),
		Invoice(77, "Hammer", 76, 11.99),
		Invoice(39, "Lawn mower", 3, 79.5),
		Invoice(68, "Screwdriver", 106, 6.99),
		Invoice(56, "Jig saw", 21, 11.00),
		Invoice(3, "Wrench", 34, 7.5)
	};
	cout << "(Original table)\n";
	cout << "Part Number\t";
	cout << setw(7) << "Part Description\t";
	cout << setw(7) << "Quantity\t";
	cout << setw(7) << "Price\n";
	cout << "----------------------------------------------------------------\n";

	for (int i = 0; i < NUM_ITEMS; i++)
	{
		cout << invoices[i].getNum();
		cout << setw(11) << "\t" << invoices[i].getDes();
		cout << setw(11) << "\t" << invoices[i].getQuan();
		cout << setprecision(2) << fixed << setw(11) << "\t$" << invoices[i].getPrice() << endl;
	}

	selectionSort(invoices, NUM_ITEMS);
	insertion_sort(invoices, NUM_ITEMS);
	getCost(invoices, NUM_ITEMS);

	return 0;
}
You set the width to be 11, and then add a tab ('\t').
But "Electric sander" is greater than 11 characters + tab cutoff, so increase your setw(11) values to at least the length of your longest string + 1, or something around there.
You can either hardcode this value, or have some function called get_longest_description(Invoice[] invoices, int Num_Items); that will find the max length for you.

PS: In the future, please post a complete and minimal example, that people can actually run, that still demonstrates your problem.
For example, try to put everything in one file (class/function definitions, etc.), and remove features that are not related to the problem at hand, such as the calls to selectionSort or insertion_sort or getCost.
Last edited on
Also placing your "output" inside code tags will help retain formatting in the forum.

You set the width to be 11, and then add a tab ('\t').

No, you are printing the tab character with that spacing. Remember that the setw() works on the next output, in this case that output is the tab character.

By the way normally when using setw() you would not normally use the tab character.

You are correct that if the "output" is larger than the setw() value nothing will appear to happen. The setw() value must be larger than the intended output before you see any difference in the output.


Right, got that confused, thanks for the correction. I agree, best to just avoid the tabs and factor that into the spacing itself.
Topic archived. No new replies allowed.