I'm not sure, but I think I remember there being something in Java that can specify how far from the left of a window that a string or digit begins..
How to easily format a table? I have this (using setw):
Bob Doe 10.96 7.61 14.39 2.11 47.30 14.21 44.58 5.00 60.23 Helen City 10.44 7.78 16.27 1.99 48.92 13.93 53.79 5.00 70.97 Joe Green 10.90 7.33 14.49 2.05 47.91 14.15 44.45 4.70 73.98
and ideally would like:
Bob Doe BLR 10.96 7.61 14.39 2.11 47.30 14.21 44.58 5.00 60.23 4:27.47 Helen City CUB 10.90 7.33 14.49 2.05 47.91 14.15 44.45 4.70 73.98 4:29.17 Joe Green USA 10.44 7.78 16.27 1.99 48.92 13.93 53.79 5.00 70.97 5:06.59
Is the only way calculations? Or is there some magical even more simple way?
Assuming you want to format your output to resemble a table, what you need is I/O manipulators. You can use setw() manipulator to set the output width and setfill() to set the filling character.
"Tabular format" is simply information presented in the form of a table with rows and columns. Most office productivity software programs, such as word processors and spreadsheets, include tools for entering text and data in tabular format.
Manipulators are used to format the data display in CPP.
In C++, you have three functions to help you do what you want. There are defined in <iomanip>
.
- setw() helps you defined the width of the output.
- setfill() Fill the rest with the character you want (in your case ' ').
- left (or right) allow you to define the alignment.
Here is the code to write your first line :
#include <iostream> #include <iomanip> using namespace std; int main() { const char separator = ' '; const int nameWidth = 6; const int numWidth = 8; cout << left << setw(nameWidth) << setfill(separator) << "Bob"; cout << left << setw(nameWidth) << setfill(separator) << "Doe"; cout << left << setw(numWidth) << setfill(separator) << 10.96; cout << left << setw(numWidth) << setfill(separator) << 7.61; cout << left << setw(numWidth) << setfill(separator) << 14.39; cout << left << setw(numWidth) << setfill(separator) << 2.11; cout << left << setw(numWidth) << setfill(separator) << 47.30; cout << left << setw(numWidth) << setfill(separator) << 14.21; cout << left << setw(numWidth) << setfill(separator) << 44.58; cout << left << setw(numWidth) << setfill(separator) << 5.00; cout << left << setw(numWidth) << setfill(separator) << 60.23; cout << endl; cin.get(); }
EDIT : To reduce the code, you can use a template function :
template<typename T> void printElement(T t, const int& width) { cout << left << setw(width) << setfill(separator) << t; }
That you can use like this :
printElement("Bob", nameWidth); printElement("Doe", nameWidth); printElement(10.96, numWidth); printElement(17.61, numWidth); printElement(14.39, numWidth); printElement(2.11, numWidth); printElement(47.30, numWidth); printElement(14.21, numWidth); printElement(44.58, numWidth); printElement(5.00, numWidth); printElement(60.23, numWidth); cout << endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With