Align cout format as table's columns
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;
}
Output aligned columns
In the class employee of print employee method:
Use this line to print.
cout << setw(20) << left << surname << setw(10) << left << empNumber << setw(4) << hourlyRate << endl;
You forgot to add "<< left
". This is required if you want left aligned.
Hope it ll useful.
C++ and table format printing
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 :)
Related Topics
Boost::Property_Tree Xml Pretty Printing
Why Can't We Declare a Namespace Within a Class
Error: Class Has Not Been Declared Despite Header Inclusion, and the Code Compiling Fine Elsewhere
What Is the Meaning of Template<> with Empty Angle Brackets in C++
Why Do We Have Reinterpret_Cast in C++ When Two Chained Static_Cast Can Do Its Job
Why Isn't There an Operator[] for a Std::List
Relative Paths Not Working in Xcode C++
Displaying Svg in Opengl Without Intermediate Raster
How to Block Running Two Instances of the Same Program
How to Iterate Over a Std::Tuple in C++ 11
Compile Openmp Programs with Gcc Compiler on Os X Yosemite
Why Doesn't C++ Make the Structure Tighter
How Are the _Cplusplus Directive Defined in Various Compilers
Are C++ Libs Created with Different Versions of Visual Studio Compatible with Each Other
What Is Assignment via Curly Braces Called? and Can It Be Controlled