| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 90581 | 2008-06-08 23:49:00 | C++ Query | jwil1 (65) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 676523 | 2008-06-08 23:49:00 | 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 (www.imagef1.net.nz), 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. |
jwil1 (65) | ||
| 676524 | 2008-06-11 05:20:00 | 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. |
Graham L (2) | ||
| 676525 | 2008-06-11 05:36:00 | 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; |
jwil1 (65) | ||
| 1 | |||||