Formatting a table

It is problably a dull question so I apologize in advance.

Can anyone get this two-row table algined, it is supposed to be aligned.
I have tried changing the number of every width in setw but I can't seem to get it aligned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<iomanip>

using namespace std;

int main(){
  double interval = 140000000;

   double i = 998989888.0, j = 858989888.0;

    cout << right << setw(20) << j << setw(3) << "  -  " << left << setw(9) << j + interval ;
    cout << right << setw(10) << 2 << endl;
    cout << right << setw(20) << i << setw(3) << "  -  " << left << setw(9) << i + interval;
    cout << right << setw(10) << 1 << endl;

}


Any help??
Last edited on
Try it with:
#include <iomanip> // recuired to use std::setw

http://www.cplusplus.com/reference/iomanip/setw/

And if you know the width approximately (for example in the case of the second column) you could also use tabs. Personally I find that easier, but tabs will not work if the difference in length exceeds one tab.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include <iomanip>      // recuired to use std::setw

using namespace std;

int main(){
  double interval = 140000000;

   double i = 998989888.0, j = 858989888.0;

    cout << right << setw(20) << j << setw(3) << "  -  " << left << setw(9) << j + interval ;
    cout << "\t" << 2 << endl;
    cout << right << setw(20) << i << setw(3) << "  -  " << left << setw(9) << i + interval;
    cout << "\t" << 1 << endl;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

using namespace std;

int main(){
  double interval = 140000000;
  double i = 998989888.0, j = 858989888.0;

  #define FMT1 << setw(12) << fixed << setprecision(1) <<
  #define FMT2 << setw(5 ) <<

  cout FMT1 j    << "  -  "    FMT1 j + interval    FMT2 2    << '\n';
  cout FMT1 i    << "  -  "    FMT1 i + interval    FMT2 1    << '\n';
}


 858989888.0  -   998989888.0    2
 998989888.0  -  1138989888.0    1
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
For someone who thought the question was dull I got real good answers, thank you guys, I really appreciate it. Specially Handy Andy, newbies (I am almost an intermediate though) are extremely amazed when they find people who go to such a lenght to explain even the most basic stuff. The programming world appreciates it.

As for the answers, @Nico, didn't even consider using tabs because
tabs will not work if the difference in length exceeds one tab.
. So I missed a possible solution for this specific problem.

lastchance , the code posted in question is a snippet from a code used in a for loop, and I use fixed and cout.presicion(). I would show it here but the loop is a little large as it has function calls inside and variable decarations for internal use.

Handy Andy, again, thank you for taking the time and explain everything in great detail.
However, I think that in the lines 33 - 36 using left and right make table look better, more readable because, for example the fields i + interval and j + interval will look better if they have the decimal places aligned. Even though the ideal is to make the numeric data right-aligned, in this particular case I favor left-alignment.
Last edited on
Hello hbcpp,

You are welcome.

Apparently your idea and my idea of a table containing numbers are two different concepts, but it is your program to do with the way you want. At least now i have a better understanding of what you want.

Andy
I'm 100% n00b. I come here to learn. The group here are kind to people, not demeaning, and always offer excellent insight. I think it's so cool that this forum appreciates the interest of the help seekers and goes above and beyond in explaining their answers in ways that makes it easy to grasp the concepts.
Topic archived. No new replies allowed.