Alignment and columns in C++ Problem

Hiya guys,

Well I need some answers regarding alignment. I have read a bit about it but I can't seem to make it become like I want.

I have put ta sample below of the output I got and the output I expect to get. You will see that the numbers in the "Square" column are not aligned well, they get our in the left :(

Anyone has a good tutorial on these alignment thing. I want to be able to master this but it doesn't seem right. If the numbers are large enough, I notice that the column get "disrupted". I appreciate all your help.

Thanks :)

Output I'm getting:
1
2
3
4
5
6
Number             Square
3                  9
4                 16
5                 25
25                625
3                  9



Output I expect:

1
2
3
4
5
6
Number             Square
3                  9
4                  16
5                  25
25                 625
3                  9




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
  #include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    int x;

    ofstream f("squares.txt");

    cout << "Enter the numbers you want followed by a 0: ";
    cin >> x;

    cout << "Number" << setw(90) << right << "Square" << endl << endl;
    f << "Number" << setw(90) << right << "Square" << endl << endl;

    while (x!=0){
        cout << x << setw(90) << right << (x*x) << endl;
        f << x << setw(90) << right << (x*x) << endl;
        cin >> x;
    }

    cout << "All data written to squares.txt" << endl << endl;
    f.close();

    return 0;
}
Try setw(a) with a < 90 !!!...a max 10...15...
Still no luck, condor. Nothing much changes to the output I expected :(
Try this:
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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    int x;

    ofstream f("squares.txt");

    cout << "Enter the numbers you want followed by a 0: ";
    cin >> x;
    cout << "Number" << setw(10) << right << "Square" << endl << endl;
    f << "Number" << setw(10) << right << "Square" << endl << endl;

    while (x!=0){
        cout << setw(10) << x << setw(10) << right << setw(10) << (x*x) << endl;
        f << setw(10) << x << setw(10) << right << setw(10) << (x*x) << endl;
        cin >> x;
    }

    cout << "All data written to squares.txt" << endl << endl;
    f.close();

    return 0;
}

Hope this helps.
I am pretty sure it is set to std::right by default.
Try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    ofstream f("squares.txt");

    f << "Number" << setw(20) <<  ' ' << "Square" << endl << endl;

    for (int x=0; x<=20; x++) 
    {
        f << setw(4) << x << setw(22) << ' ' << setw(5) << (x*x) << endl;
    }

    cout << "All data written to squares.txt" << endl << endl;
    f.close();

    return 0;
}

Output:
Number                    Square

   0                          0
   1                          1
   2                          4
   3                          9
   4                         16
   5                         25
   6                         36
   7                         49
   8                         64
   9                         81
  10                        100
  11                        121
  12                        144
  13                        169
  14                        196
  15                        225
  16                        256
  17                        289
  18                        324
  19                        361
  20                        400
Thanks people :D
I managed to figure it out as well :)

Here is my code:

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    int x;

    ofstream f("squares.txt");

    cout << "Enter the numbers you want followed by a 0: ";
    cin >> x;

    cout <<  left << setw(15) << "Number";
    cout <<  left << setw(15) << "Square" << endl;

    f <<  left << setw(15) << "Number";
    f <<  left << setw(15) << "Square" << endl;


    while (x!=0){
        cout << left << setw(15) << x;
        cout << left << setw(15) << x*x << endl;

        f << left << setw(15) << x;
        f << left << setw(15) << x*x << endl;

        cin >> x;
    }

    cout << "All data written to squares.txt" << endl << endl;
    f.close();

    return 0;
}
He wants it to be aligned to the left. Something like 10 width and values of 3 , 10 , 112 would look like


       3
       10
       112


What I would do is something like

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

int main()
{
	const int indent = 20;
	int extra= 0;
	std::cout << std::setfill( ' ' );
	std::cout << "Number" << std::setw( indent ) << "Square" << std::endl;
	
	for( int i = 0; i < 20; ++i )
	{
		if( i < 10 ) extra = 0;
		else if( i < 100 ) extra = 1;
		std::cout << i << std::setw( indent - 1 - extra ) << ' ';
		std::cout <<  i * i << std::endl; 
	}
	return( 0 );
}
Number              Square
0                   0     
1                   1     
2                   4     
3                   9     
4                   16    
5                   25    
6                   36    
7                   49    
8                   64    
9                   81    
10                  100   
11                  121   
12                  144   
13                  169   
14                  196   
15                  225   
16                  256   
17                  289   
18                  324   
19                  361   



*added output and this link - http://ideone.com/Gk3DZD

**had a line of code that wasn't needed.
Last edited on
Topic archived. No new replies allowed.