Data Table

I made this Data Table.

The problem is when the outputs are different it will change the shape of the Table.

I need some tips on how can i fix this problem.

Thanks in Advance.

double sum = 0;

for ( int j = 0 ; j < 5 ; j++ )
sum += myClass[j].getTotalToPay();

cout << " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
cout << " x First | ID | Pay-Rate | Worked-Hours | Over-Time | TotalPay x" << endl;
cout << " x Name | Number | | ( Hours ) | (Hours) | ( $ ) x" << endl;
cout << " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;


for ( int i = 0 ; i < 5 ; i++ )
{
cout << " " << myClass[i].getName()
<< setw(9) << myClass[i].getNumber()
<< setw(11) << myClass[i].getRate()
<< setw(14) << myClass[i].getWorked()
<< setw(17) << myClass[i].getOverTime()
<< setw(13) << "$" << myClass[i].getTotalToPay() << endl;
}
cout << " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
cout << " Totals x $" << sum << endl;
cout << " xxxxxxxxxxxxxx" << "\n\n\n";
[1586 byte] By [Max_Payne] at [2007-11-20 11:33:31]
# 1 Re: Data Table
It seems like what you need to do is to subtract the actual size of the cell's contents from the spacing.

So you have a fixed offset of 9 chars for the "ID Number" field, if you have a name that is 5 chars you know you have a remainder of 4, so instead of moving over 9 by spitting out 9 spaces (which I assume is what the setw(9) function does), you only need to spit out 4 spaces after the 'name' string to reach the start of the ID Number field. So you'd need to get the length of the previous output string (maybe with strlen() or whatever is convenient) and then subtract that from your known offsets.

One possible limit of that is if you have a string that is longer than your column width, in which case you can take the width and only output the maximum possible size if it's too large (cutting off the string).

If you wanted to center the string in the columns then you'd need to take the string length, get the remainder (e.g. a name string of length 6, in a column of size 9, leaves 3), and split that amongst the sides. That means having two setw() calls, one for 1 char, then spit out the name string, then another set() with the other 2 spaces needed to pad out the column.

Edit: I didn't realize the setw() call was a standard c++ library call... I've never used it, so what I said might be gibberish (or just the old crusty way of doing things).
wildfire99 at 2007-11-10 22:25:01 >
# 2 Re: Data Table
Can you give me an example of that ?

& yes setw() is in the <iomanip> header file.

eg:

setw(5) is the same as 5 cout << " " ; spaces.
Max_Payne at 2007-11-10 22:26:10 >