Formatting output in a table, C ++

How can I output data to the console in a table in C ++? There is a question for this in C #, but I need it in C ++.

This, with the exception of C ++: How: The best way to draw a table in a console application (C #)

+8
c ++ format console tabular
source share
4 answers

Could you do something very similar to the C # example:

String.Format("|{0,5}|{1,5}|{2,5}|{3,5}|", arg0, arg1, arg2, arg3); 

how

 printf("|%5s|%5s|%5s|%5s|", arg0, arg1, arg2, arg3); 

Here is the link I used for this: http://www.cplusplus.com/reference/clibrary/cstdio/printf/

+5
source share

Here is a small example of what iomanip has:

 #include <iostream> #include <iomanip> int main(int argc, char** argv) { std::cout << std::setw(20) << std::right << "Hi there!" << std::endl; std::cout << std::setw(20) << std::right << "shorter" << std::endl; return 0; } 

There are other things you can do, for example, setting the precision of floating point numbers, changing the character used as a complement when using setw, displaying numbers in something other than base 10, etc.

http://cplusplus.com/reference/iostream/manipulators/

+7
source share

I could not find something that I liked, so I did it. Find it https://github.com/haarcuba/text-table

Here is an example of its output:

 +------+------+----+ | |Sex | Age| +------+------+----+ |Moses |male |4556| +------+------+----+ |Jesus |male |2016| +------+------+----+ |Debora|female|3001| +------+------+----+ |Bob |male | 25| +------+------+----+ 
+2
source share

Check the length of the column value, and also keep the length of the value for the format.

 printf(" %-4s| %-10s| %-5s|\n", "ID", "NAME", "AGE"); 

See how the MySQL shell interface was designed, this will give you a good idea.

0
source share

All Articles