Formatting help (iomanip)

Apr 30, 2015 at 4:07pm
I'm working on a program that will allow an unknown number of people to input their information in a file, and then that information would be output in a new file. The problem is that the formatting looks awful, and I can't seem to get all the information to neatly line up in columns.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 void printData(string first[], string last[], int id[], string email[], int size){
	int i;

	cout << setiosflags(ios::fixed | ios::showpoint | ios::right);
	cout << setprecision(2);

	ofstream outData;
	outData.open("sigs.txt");

	outData << "ID" << setw(20) << "Full Name" << setw(20) << "Email" << endl;

	for (i = 0; i < size; i++){
		outData << id[i] << setw(18) << last[i] << ", " << first[i] << setw(39) << email[i] << endl;
	}

	outData << "Number of signatures: " << size;

	cout << "The file has been written." << endl;
}
Last edited on Apr 30, 2015 at 4:07pm
May 1, 2015 at 5:48am
Note that setw sets the width of the next output. You are not setting the width for the ID column.

When you have multiple strings in the same column you will have to concatenate them so that you can output it all in a single call to operator<<.
 
... << setw(18) << (last[i] + ", " + first[i]) << ...
Last edited on May 1, 2015 at 5:48am
Topic archived. No new replies allowed.