PDA

View Full Version : C++ Query



jwil1
09-06-2008, 11:49 AM
Hi all,

I want to create a simple table in a C++ console program.

This is my code (to print a single record from temp - this is in a loop):


cout << setw( 10 ) << left << temp.fName
<< setw( 10 ) << temp.lName
<< setw( 8 ) << temp.age
<< setw( 8 ) << temp.pcBrand
<< setw( 8 ) << temp.monitorType
<< setw( 1 ) << right << "$" << temp.amountSpent
<< setw( 30 ) << temp.lastModified;


The problem is, the last category (temp.lastModified) is out of alignment with the other categories (http://www.imagef1.net.nz/files/setw.jpg), depending on the size of the temp.amountSpent field for each record.

I have been using set-width (setw()) to align the rest of the data - this works correctly - but the last category will not align correctly.

How can I make it so the last category will line up vertically?

TIA

EDIT: The names I have blanked out are different lengths.

Graham L
11-06-2008, 05:20 PM
It looks as if you get a fixed number of spaces after each field. If the earlier columns are always the same length, they are no problem.

Why not start the last field with a <TAB> character? That should line it to the next (fixed) tab position.

jwil1
11-06-2008, 05:36 PM
Thanks Graham but I did this and now it works:

cout << setw( 10 ) << left << temp.fName
<< setw( 10 ) << temp.lName
<< setw( 8 ) << temp.age
<< setw( 8 ) << temp.pcBrand
<< setw( 8 ) << temp.monitorType
<< setw( 1 ) << "$" << setw( 7 ) <<temp.amountSpent
<< setw( 28 ) << right << temp.lastModified;