Now that you mention it, you're right. Using setw() for debtors[].amount is unnecessary because the output is left justified.
For what it's worth, the following might help to picture what's going on here (or perhaps you've already got it?);
If you think of setw() as doing something similar to defining the column widths of a table, then defining the width of the debtors[].amount field gives;
1 2 3 4 5 6 7
|
first "column" second "column" third "column"
defined by the defined by the defined by the
first set(w) second set(w) third set(w)
X========================X========================X========================X
X " " X debtors[j].name X debtors[j].amount X
X========================X========================X========================X
|
I think it should be easy to see that if the contents of the third "column" are left justified, then it doesn't really matter whether this "column" exists or not. What you've done is essentially the following -- and it works just as well;
1 2 3 4 5 6 7
|
first "column" second "column" no third "column"
defined by the defined by the
first set(w) second set(w)
X========================X========================X
X " " X debtors[j].name X debtors[j].amount
X========================X========================X
|
Only if you wanted to output another value after debtors[].amount would you need to add another "column" to this imaginary table (you need to keep in mind that this talk of tables and columns is just a helpful way of thinking about what set(w) is doing).
The reason the you were still having formating problems before adding ' << setw(fieldwidth) << " " ' can be seen if you remove the first "column";
1 2 3 4 5 6 7
|
first "column" second "column"
defined by the defined by the
first set(w) second set(w)
X========================X========================X
X debtors[j].name X debtors[j].amount X
X========================X========================X
|
Here the output of the first field, being left justified, pushes it right up against the edge of the output window.
All that said, you don't even need the first setw() - you could set the position of the debtors[].name field simply by padding the output with spaces;
1 2 3 4 5 6
|
for (int j =0; j < 5;j++)
{
cout << " " <<
setw(18) << left << debtors[j].name <<
debtors[j].amount << "\n\n";
}
|
which gives;
1 2 3 4 5 6 7
|
first "column" second "column" no third "column"
defined by defined by
spaces set(w)
" "X========================X
" "X debtors[j].name X debtors[j].amount
" "X========================X
|
Anyway, glad it all worked out in the end!