Output Alignment

how can i put a table with colums using c++
Last edited on
I personally have little to zero experience using setw (heck, I personally find it more fun to create my own tools) so I'll show you a different, clever method that you can use.

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

using namespace std;

void Display_Formatted_Text (const string & szOne, const string & szTwo);

int main()
{	
	Display_Formatted_Text ("Price", "Quantity");
	Display_Formatted_Text ("12.99", "5");
	Display_Formatted_Text ("9.99", "10");
	cin.get();

	return 0;
}

void Display_Formatted_Text (const string & szOne, const string & szTwo)
{
	cout << szOne;
	for (unsigned int i = szOne.length(); i < 15; i++) // can change 15 to whatever you'd like
		cout << " ";
	cout << szTwo << endl;
}


If you require 3 columns, then you can simply overload the function.
Last edited on
Double-nested for loop:

1
2
3
4
5
6
7
8
9
int i, j;
for (i = 0; i < m; i++)
{
     for (j = 0; j < n; j++)
     {
          // ... print row data separated by tabs or spaces
     }
     cout << endl;
}
Simple solution:
1
2
3
4
5
6
7
8
struct DATA
{
  double a;
  int b;
} data[10];

for (i = 0; i < 10; ++i)
    std::cout << std::setw(10) << data[i].a << std::setw(8) << data[i].b << std::endl;
Last edited on
Highly Formating Table
a short cutting from my, mysql type program header's source 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//can change *'s to +'s
//Using these with for loop and user input can creeate Mysql type environment
#include<iostream>
#include<windows.h>
#include<string>

using namespace std;

int row,col,tsize;
int max_x=80,max_y=50;   //console size

void Table(int,int,int size=8);
void Header(string,int);
void Data(string,int,int);

//Format data
void gotoxy(int, int);
void coldraw(int);
void Format(string Message, char T='M', int F=1);

int main()
{
	Table(5,4);
	Header("No.",1);
	Header("Name",2);
	Header("Marks",3);
	Header("Percentage",4);
	Data("1",1,1);
	Data("2",2,1);
	Data("3",3,1);
	Data("4",4,1);
	Data("Dust",1,2);
	Data("Must",2,2);
	Data("Rust",3,2);
	Data("Just",4,2);
	cin.get();
	return 0;
}

void Table(int r, int c, int size)
{
	if(r<=0 || c<=0 || size<=0 || r>25 || c>10 || size>10 || (c>5 && size>5))
	{
		Format("Table size error!!!!");
		return;
	}
	row=r;
	col=c;
	tsize=size;
	gotoxy(max_x/2-(c*(size+1))/2,1);
	for(int i=0;i<=c*(size+1);i++)   //<= as column lines are +1 no of coloumns
		cout<<"*";
	gotoxy(max_x/2-(c*(size+1))/2,3);
	for(int i=0;i<=c*(size+1);i++)
		cout<<"*";
	for(int i=0;i<=c;i++)
	{
		coldraw(i);
	}
	gotoxy(max_x/2-(c*(size+1))/2,r+4);
	for(int i=0;i<=c*(size+1);i++)
		cout<<"*";
}

void coldraw(int colno)
{
	for(int i=1;i<=row+4;i++)
	{
		gotoxy(max_x/2-(col*(tsize+1))/2+colno*(tsize+1),i);
		cout<<"*";
	}
}

void Header(string head,int colno)
{
	colno--;
	gotoxy(max_x/2-(col*(tsize+1))/2+colno*(tsize+1)+1,2);  //+1 for start
	if((int)head.size()>tsize)
	{
		cout<<head.substr(0,tsize);
		return;
	}
	for(int i=1;i<=(tsize-head.size())/2;i++)
		cout<<" ";
	cout<<head;
}

void Data(string data,int r,int c)
{
	c--;
	gotoxy(max_x/2-(col*(tsize+1))/2+c*(tsize+1)+1,r+3);  //+1 for start
	if((int)data.size()>tsize)
	{
		cout<<data.substr(0,tsize);
		return;
	}
	for(int i=1;i<=(tsize-data.size())/2;i++)
		cout<<" ";
	cout<<data;
}

void Format(string Message, char T, int F)
{
	if(T=='M')
	{
		gotoxy(max_x/2-Message.size()/2,max_y/2);
		cout<<Message;
	}
	else if(T=='F')
	{
		//other code
	}
}

void gotoxy(int x, int y)
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	_COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hConsole, pos);
}
Last edited on
Topic archived. No new replies allowed.