PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > iomanip - alignment
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
iomanip - alignment

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 22h04   #1
brekehan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut iomanip - alignment

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.

  Réponse avec citation
Vieux 18/10/2007, 23h51   #2
Maarten Kronenburg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: iomanip - alignment


"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;


  Réponse avec citation
Vieux 19/10/2007, 10h10   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: iomanip - alignment

On Oct 18, 11:04 pm, brekehan <cp...@austin.rr.com> wrote:
> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h48.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10127 seconds with 11 queries