C++ <iomanip> problem.

d0ofy

Golden Member
Oct 11, 1999
1,404
0
0
I'm trying to output data in a table using a FOR loop. The first field is the Name, and I want it aligned left, so I use setiosflags(ios::left). The rest of the fields I want aligned right, so I used setiosflags(ios::right), but it still aligns left. I also tried resetiosflags(ios::right), and that doesn't work either. The whole table aligns left. Please help! Thanks.
 

mjquilly

Golden Member
Jun 12, 2000
1,692
0
76
You aren't outputting everything into the same column are you? Make sure to use setw(xx) before each field is output. ie:

cout << setiosflags(ios::left) << setw(10) << Name << setiosflags (ios::riht) << setw (10) << val1 << setw(10) << val2 << ........ << setw(10) << valn << endl;
 

d0ofy

Golden Member
Oct 11, 1999
1,404
0
0
That's exactly what I did.
Does it matter if the setiosflags() goes before the setw()? Or can I put the setw() before the setiosflags()?
 

mjquilly

Golden Member
Jun 12, 2000
1,692
0
76
I don't think it matters what order they go in, as long as you set your alignment before outputting the variable. The only other thing I can think of is to make sure you are making your column widths big enough.

cout << setiosflags(ios::left) << setw(8) << &quot;mjquilly&quot; << setiosflags(ios::right) << setw(2) << &quot;is&quot; << setw(7) << &quot;awesome&quot; << endl;

that would print:
&quot;mjquillyisawesome&quot;
 

d0ofy

Golden Member
Oct 11, 1999
1,404
0
0
That didn't work either.
But I figured it out. Instead of setiosflags(), I used cout.setf(ios::left), then unsetf(ios:left). Works perfectly.
Thanks a lot for your input though.