• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ <iomanip> problem.

d0ofy

Golden Member
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.
 
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;
 
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()?
 
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;
 
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.
 
Back
Top