Column Headers

Feb 15, 2014 at 10:21pm
Hi there, I'm basically a computer dummy taking my first computer science class. I'm working on a project that requires the output to be placed into columns. Getting the data into the columns is easy enough, but how do I place titles over the columns?

The output is supposed to look like this:

Weight Allowances

lightweight heavyweight
data 1 / xxxx / xxxx
data 2 / xxxx / xxxx
data 3 / xxxx / xxxx
...

How do I add in Weight Allowances, heavyweight and lightweight?
Last edited on Feb 15, 2014 at 10:37pm
Feb 15, 2014 at 10:41pm
There should be spaces between the datas and the xes and lightweight and heavyweight should be above the xes. I tried entering them, but it all got squished together when I hit submit.
Feb 15, 2014 at 10:43pm
You can use setw from <iomanip> to format the output into columns.

More info here...
http://www.cplusplus.com/reference/iomanip/setw/
Feb 15, 2014 at 10:58pm
Thanks for your reply. I know how to get my output into columns, it's giving those columns titles that I don't know how to do. For example, let's say I was making a program that converted some figure into both metric and English sizes and I wanted to put those figures into columns.

English metric
thing 1 xxx xxx
thing2 xxx xxx
...

How would I get it so that English and metric will appear above each of the columns?
Feb 15, 2014 at 11:24pm
Something like this? I'm assuming the data in the table will be shorter than the heading titles, so that drives how wide the setw is set.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
	
	cout<< setw(10) <<" " <<"\t"<<"lightweight"<<"\t"<<"heavyweight"<<endl;
	cout<< setw(10) <<"thing1" <<"\t"<<setw(11)<<"data1"<<"\t"<<setw(11)<<"data2"<<endl;
	cout<< setw(10) <<"thing2" <<"\t"<<setw(11)<<"data1"<<"\t"<<setw(11)<<"data2"<<endl;
	return 0;
}


Feb 16, 2014 at 2:30am
Awesom, that's exactly what I wanted. Thank you so much!
Topic archived. No new replies allowed.