Two lines of table not aligning?

What can I do to make these two items print inline with the rest of the table? I have been messing with my setw but I can't figure it out.

Current output:
Part Number Part Description Quantity Price
----------------------------------------------------------------
83 Electric sander -------- 7 ------- $57.98 (added lines to show)
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
Press any key to continue . . .

Desired output:
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
Press any key to continue . . .
1
2
3
4
5
6
7
8
9
10
11
12
13
	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;
	}
Last edited on
setw sets the minimum width of whatever comes after so you probably want to use it before outputting invoices[i].getNum() too.
You need to repost your report layout since it's spacing has been squished.
Put it in output tags (like code tags, but with the word output instead).

The setw() calls should be right before the thing being printed; i.e., don't print a '\t' in between.
I don't think that will fix the problem I'm encountering. I am happy with the table except for the two parts.
What two parts? How are we supposed to figure that out? Have you even looked at your post? Look at it. Could you figure it out? And you're too lazy to just repost the table like I asked so we can actually see what's going on.
I was responding to someone else lol r you ok
Hello stormbot,

See if this gives you any ideas:
1
2
3
4
5
std::cout << std::setw(4) << ' ' << std::setw(2) << invoices[i].getNum()
	<< std::setw(10) << ' ' << std::left << std::setw(20) << invoices[i].getDes()
	<< std::setw(5) << ' ' << std::right << std::setw(4) << invoices[i].getQuan()
	<< std::setw(11) << " $" << std::setw(6) << invoices[i].getPrice()
	<< std::endl;


I used your original code to for the column headings.

This is the output it produces:
Part Number     Part Description        Quantity        Price
    83          Electric sander             7          $ 57.98
    24          Power saw                  18          $ 99.99
     7          Sledge hammer              11          $  21.5
    77          Hammer                     76          $ 11.99
    39          Lawn mower                  3          $  79.5
    68          Screwdriver               106          $  6.99
    56          Jig saw                    21          $    11
     3          Wrench                     34          $   7.5


 Press Enter to continue


Hope that helps,

Andy
My previous response was a bit rushed so I give it a new try.

I agree with tpb that you probably should not use \t in your output. Mixing it with setw will not do what you expect.

Note that setw applies only to the output that comes right after, so setw(11) on the last line is applied to "\t$" and then invoices[i].getPrice() is outputted after without a specified output width. If you want the column to be left aligned you might not need to use setw here because it's the last thing on the line, but otherwise you might want to generate a string containing both the dollar sign and the price number so that you can output them together.

I am by no means an expert when it comes to creating nice looking tables with cout and setw but here is my attempt:

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << setw(18) << left << "Part Number";
cout << setw(18) << left << "Part Description";
cout << setw(18) << left << "Quantity";
cout << setw(18) << left << "Price";
cout << "\n----------------------------------------------------------------\n";

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


I made all columns 18 characters wide. You can of course change the numbers so that the different columns have different widths. Just make sure to use the same width for both the header and the data.

I also made all columns left aligned for simplicity. You might want to make the numerical columns right aligned so that the digits align more nicely but then you might need to add some extra space between the columns otherwise a right aligned column followed by a left aligned column will not look nice because they are too close together.
Topic archived. No new replies allowed.