iomanip - alignment

B

brekehan

I can't seem to get the alignment to switch back and forth combined
with a set width. I tryed a search on this group and alot of googling,
but still can't deduce the problem.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
// using cout instead of ostream & operator << method, for example
purposes
// hardcoding values instead of class data for example purposes

cout << setiosflags(ios::fixed);

cout << left << setw(25) << "attribute name:";
cout << right << setw( 6) << 0.111;
cout << endl;

cout << left << setw(25) << "attribute name";
cout << right << setw( 6) << 0.123556;
cout << endl;

// etc etc

cout << resetiosflags(ios::fixed);

return 0;
}

The output seems to always be left aligned. I've also tryed using <<
setiosflags and << resetiosflags with the same results.

Can anyone clear this up for me? I hate to admit that I've forgotten
something so basic.

What is the lifetime of the differant iomanip members used here?
How are right and left alignments represented internally? i.e do you
need to reset left before using right and vica versa? or are they two
states of the same bit.
 
M

Maarten Kronenburg

"brekehan" wrote in message
I can't seem to get the alignment to switch back and forth combined
with a set width. I tryed a search on this group and alot of googling,
but still can't deduce the problem.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
// using cout instead of ostream & operator << method, for example
purposes
// hardcoding values instead of class data for example purposes

cout << setiosflags(ios::fixed);

cout << left << setw(25) << "attribute name:";
cout << right << setw( 6) << 0.111;
cout << endl;

cout << left << setw(25) << "attribute name";
cout << right << setw( 6) << 0.123556;
cout << endl;

// etc etc

cout << resetiosflags(ios::fixed);

return 0;
}

The output seems to always be left aligned. I've also tryed using <<
setiosflags and << resetiosflags with the same results.

Can anyone clear this up for me? I hate to admit that I've forgotten
something so basic.

For the floating point you also may use setprecision(3) for 3 decimals after
the point:
cout << right << setw(6) << setprecision(3) << 0.123;
 
J

James Kanze

I can't seem to get the alignment to switch back and forth combined
with a set width. I tryed a search on this group and alot of googling,
but still can't deduce the problem.

It works fine for me, both with g++ and with Sun CC. However...
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// using cout instead of ostream & operator << method, for example purposes
// hardcoding values instead of class data for example purposes
cout << setiosflags(ios::fixed);

I'm not sure that this is defined behavior. I'd normally use
std::cout << std::fixed ;
or
std::cout.setf( std::ios::fixed, std::ios::floatfield ) ;

(In a real program, of course, you'll almost always be using
custom manipulators, defined in accordance with the semantics of
the data being output.)
cout << left << setw(25) << "attribute name:";
cout << right << setw( 6) << 0.111;

Note that the setw here doesn't do anything, since you're
generating more than six characters anyway ("0.111000").
cout << endl;
cout << left << setw(25) << "attribute name";
cout << right << setw( 6) << 0.123556;

Same comment as above for the setw. This generates "0.123556".
cout << endl;
// etc etc
cout << resetiosflags(ios::fixed);

Again, this isn't guaranteed to do what you think. "std::cout
<< std::resetiosflags( ios::floatfield )" would do the trick,
"std::cout << std::defaultfloat" is probably more readable, but
normally, if the goal is to restore the previous context, you'll
read the previous context before changing it, typically by means
of some sort of RAII class. (Of course, if you're using custom
manipulators, you'll arrange for them to restore the original
state at the end of the full expression, so this won't be
necessary.)
return 0;

}
The output seems to always be left aligned.

Try putting markers around it, and I think you'll see what the
problem is. Something like:
std::cout << '|' << std::left << std::setw( 25) << "attr:" <<
'|' ;
std::cout << '|' << std::right << std::setw( 6 ) << 1.23 << '|' ;
This will show exactly how each field is being formatted.
I've also tryed using <<
setiosflags and << resetiosflags with the same results.
Can anyone clear this up for me? I hate to admit that I've forgotten
something so basic.
What is the lifetime of the differant iomanip members used here?
How are right and left alignments represented internally? i.e do you
need to reset left before using right and vica versa? or are they two
states of the same bit.

Alignment is a field in ios, consisting of at least 2 bits. To
assign to it, you have to do something like:

std::setf( std::ios::left, std::ios::alignfield ) ;

The second argument tells setf to reset the bits first. This is
exactly what std::left does, however, so there should be no
problem using the manipulator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top