Hello hbcpp,
Nico's link is a good start, but you may find it a bit lacking in information.
Some things that the link does not tell you,
When using "setw()" right justification is the default unless yo change it by using "left".
The size of "setw()" must be large enough to include all the characters that need to be in the "setw" block. When dealing with numbers this means all numbers to the left of the decimal point + the decimal point + any numbers to the right of the decimal point. Otherwise you could exceed the "setw()" block and the alignment will be off.
When dealing with numbers right justification is the best. Along with setprecision this will align the decimal points in the same column. Or whole numbers to the right with padding on the left.
This should give you an idea of how it all works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include<iostream>
#include<iomanip>
using namespace std; // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
// See output on the right --->
int main()
{
double interval = 140000000;
double i = 998989888.0, j = 858989888.0;
std::cout << std::fixed << std::setprecision(1);
std::cout << std::setfill('*');
cout << right << setw(20) << j << setw(3) << " - " << left << setw(12) << j + interval;
cout << ' ' << right << setw(10) << 2 << endl;
cout << right << setw(20) << i << setw(3) << " - " << left << setw(12) << i + interval;
cout << ' ' << right << setw(10) << 10 << endl;
std::cout << "\n\n";
cout << /*right <<*/ setw(20) << j << setw(3) << " - " << /*left <<*/ setw(12) << j + interval;
cout << ' ' << /*right <<*/ setw(10) << 2 << endl;
cout << /*right <<*/ setw(20) << i << setw(3) << " - " << /*left <<*/ setw(12) << i + interval;
cout << ' ' << /*right <<*/ setw(10) << 10 << endl;
std::cout << "\n\n" << std::setfill(' ');
cout << /*right <<*/ setw(20) << j << setw(3) << " - " << /*left <<*/ setw(12) << j + interval;
cout << /*right <<*/ setw(10) << 2 << endl;
cout << /*right <<*/ setw(20) << i << setw(3) << " - " << /*left <<*/ setw(12) << i + interval;
cout << /*right <<*/ setw(10) << 10 << endl;
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
|
*********858989888.0 - 998989888.0* *********2
*********998989888.0 - 1138989888.0 ********10
*********858989888.0 - *998989888.0 *********2
*********998989888.0 - 1138989888.0 ********10
858989888.0 - 998989888.0 2
998989888.0 - 1138989888.0 10
Press Enter to continue: |
Line 17 changes the fill character so you can see how the "setw" block is used and where the unused portion is filled.
In lines 19 - 29 I added an extra space so that each "setw" block stands out. Also change the (1) to (10) to show how that works.
Lines 26, 28, 33 and 35 I changed the "setw" so the block is large enough to include all numbers.
Lines 33 - 36 are what your original code should look like. Your use of "left" and "right" does not give you the correct alignment of your numbers.
I do like
lastchance's use of the "#define"s although the "fixed << setprecision(1)" part only need to be done once and it affect each "cout' statement until it is changed or the program ends.
Andy