6

I am looking for how to print in C++ so that table column width is fixed. currently I have done using spaces and | and -, but as soon as number goes to double digit all the alignment goes bad.

|---------|------------|-----------|
| NODE    |   ORDER    |   PARENT  |
|---------|------------|-----------|
|  0      |     0      |           |
|---------|------------|-----------|
|  1      |     7      |     7     |
|---------|------------|-----------|
|  2      |     1      |     0     |
|---------|------------|-----------|
|  3      |     5      |     5     |
|---------|------------|-----------|
|  4      |     3      |     6     |
|---------|------------|-----------|
|  5      |     4      |     4     |
|---------|------------|-----------|
|  6      |     2      |     2     |
|---------|------------|-----------|
|  7      |     6      |     4     |
|---------|------------|-----------|
Avinash
  • 12,851
  • 32
  • 116
  • 186
  • It sounds like you need to define a default width for your printing format. Can you show us how you are printing. `std::cout` or `printf()` – linuxuser27 Oct 05 '10 at 19:47
  • See my solution here: [http://stackoverflow.com/a/20924887/1325279](http://stackoverflow.com/a/20924887/1325279) – synaptik Jan 04 '14 at 18:26

3 Answers3

14

You can use the std::setw manipulator for cout.

There's also a std::setfill to specify the filler, but it defaults to spaces.

If you want to center the values, you'll have to do a bit of calculations. I'd suggest right aligning the values because they are numbers (and it's easier).

cout << '|' << setw(10) << value << '|' << setw(10) << value2 << '|' << endl;

Don't forget to include <iomanip>.

It wouldn't be much trouble to wrap this into a general table formatter function, but I'll leave that as an exercise for the reader :)

JoshD
  • 12,490
  • 3
  • 42
  • 53
  • @Avinash: "What's wrong with this" isn't really a good question. Are you getting a compilation error? Is it formatted wrong? Please provide more. You can update your question with this... – JoshD Oct 05 '10 at 20:22
  • During parallel / multicore programs, this will get screwed up for sure. – Nicholas Hamilton Oct 02 '13 at 06:21
8

You can use the beautiful printf(). I find it easier & nicer for formatting than cout.

Examples:

int main()
{
    printf ("Right align: %7d:)\n", 5);
    printf ("Left align : %-7d:)\n", 5);

    return 0;
}
Donotalo
  • 12,748
  • 25
  • 83
  • 121
  • Avinash's comment to the accepted solution nicely highlights precisely why printf() is far superior to cout for formatted output. ;) – dandan78 Oct 05 '10 at 20:38
  • 1
    @dandan78 Which comment? :D – Muhamed Huseinbašić Oct 23 '15 at 18:23
  • I completely agree that `printf` is far more convenient than all that `cout` torture. .NET's `Format` isn't much better, and Java's `print/prinln` a catastrophy. I could regret good old `print using` in Basic. –  Jan 05 '23 at 20:39
2

Might as well give the C++20 std::format answer, since we already have the C++ std::cout and printf() answers.

std::cout << std::format( "|{:^9}|{:^9}|{:^9}|\n", node, order, parent );

OP, your column widths are all different sizes. Here I made them all 9. Also, horizontal lines don’t really increase legibility, but they use up a lot of space. I would personally get rid of them.

| NODE    | ORDER   | PARENT  |
|   11    |    7    |    3    |
|   12    |    5    |   11    |

Formatting stuff this way is really easy with std::format. Be sure to check out the documentation.

Oh yeah, don’t forget to:

#include <format>
#include <iostream>
Dúthomhas
  • 8,200
  • 2
  • 17
  • 39