Align cout format as table's columns
Asked Answered
M

5

32

I'm pretty sure this is a simple question in regards to formatting but here's what I want to accomplish:

I want to output data onto the screen using cout. I want to output this in the form of a table format. What I mean by this is the columns and rows should be properly aligned. Example:

Test                 1
Test2                2
Iamlongverylongblah  2
Etc                  1

I am only concerned with the individual line so my line to output now (not working) is

cout << var1 << "\t\t" << var2 << endl;

Which gives me something like:

Test                 1
Test2                  2
Iamlongverylongblah         2
Etc                  1
Mouse answered 9/11, 2008 at 1:40 Comment(0)
C
57

setw.

#include <iostream>
#include <iomanip>
using namespace std;

int main () {
  cout << setw(21) << left << "Test"    << 1 << endl;
  cout << setw(21) << left << "Test2"   << 2 << endl;
  cout << setw(21) << left << "Iamlongverylongblah"     << 2 << endl;
  cout << setw(21) << left << "Etc"     << 1 << endl;
  return 0;
}
Clere answered 9/11, 2008 at 1:51 Comment(2)
You forgot to add "<< left". This is required if you want left-aligned fixed fields.Pinta
std::left is not reset on every formatted output, you only need it once. (The stream's width is reset.)Sandbank
V
10

I advise using Boost Format. Use something like this:

cout << format("%|1$30| %2%") % var1 % var2;
Vala answered 9/11, 2008 at 1:49 Comment(0)
P
2

You must find the length of the longest string in the first column. Then you need to output each string in the first column in a field with the length being that of that longest string. This necessarily means you can't write anything until you've read each and every string.

Planetoid answered 9/11, 2008 at 1:51 Comment(3)
Isn't there a easier way? Using setw or something.Mouse
>Isn't there a easier way? Not unless you can predict the future. >Using setw or something. Yes, setw is one way to "output each string in the first column in a field with the length being that of that longest string."Buehler
Most formatted output I've seen doesn't bother to find the max size of a field and if it overruns a decent value, oh well, but the formatting looks a little weird when it does.Lorinalorinda
N
2

You might want to use std::format

with alignment syntax "{:<30} | {:<30}\n". This enables you to format your text using an aligment with a specific width.

To format a table you would append to a std:string table_rows and format the elements with alignment and width, like so:

table_rows += format("{:<32} | {:<30}\n", str_col_1, str_col_2);

For debugging some fill-in chars might come handy, which you can set like this: format("{:*<32} | {:+<30}\n" ... .

In case you really need to work with dynamic width, then determine the longest "str_col_1" and use it as another parameter to format:

std::format("{:<{}}", "left aligned", 30);

Example:

#include <iostream>
#include <string>
#include <fmt/format.h>

int main()
{
    std::string table_rows;
    std::string str_col_1[] = {"Name", "Age", "Gender", "City"};
    std::string str_col_2[] = {"John", "25", "Male", "New York"};

    for (int i = 0; i < 4; i++) {
        table_rows += fmt::format("{:<10} | {:<15}\n", str_col_1[i], str_col_2[i]);
    }

    std::cout << table_rows << "\n";

    return 0;
}

Output:

Name       | John           
Age        | 25             
Gender     | Male           
City       | New York 

Referencing:

https://en.cppreference.com/w/cpp/utility/format/formatter https://fmt.dev/latest/syntax.html

Nathalienathan answered 30/3, 2023 at 0:16 Comment(0)
T
0

you can do it with

string str = "somthing";
printf ("%10s",str);
printf ("%10s\n",str);
printf ("%10s",str);
printf ("%10s\n",str);
Trihydric answered 15/12, 2013 at 16:46 Comment(3)
Surely he already knows that with "printf" it can be done. He is specifically asking for "cout".Cassicassia
How do you know what he (OP) knows? OP never said he tried printf, nor that he wants just cout based answer (-(-1)).Agreeable
cout is in the title and they wrote: "I want to output data onto the screen using cout"Berley

© 2022 - 2024 — McMap. All rights reserved.