BobS
BobS

Reputation: 567

Align cout format as table's columns

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

Upvotes: 32

Views: 76163

Answers (5)

Jens A. Koch
Jens A. Koch

Reputation: 41756

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

Upvotes: 2

avishayhajbi
avishayhajbi

Reputation: 77

you can do it with

string str = "somthing";
printf ("%10s",str);
printf ("%10s\n",str);
printf ("%10s",str);
printf ("%10s\n",str);

Upvotes: 0

Eugene Yokota
Eugene Yokota

Reputation: 95624

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

Upvotes: 58

Leon Timmermans
Leon Timmermans

Reputation: 30225

I advise using Boost Format. Use something like this:

cout << format("%|1$30| %2%") % var1 % var2;

Upvotes: 10

sgm
sgm

Reputation:

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.

Upvotes: 2

Related Questions