Topic: iostream and iomanip
Author: "Steve Mann" <smann@bridge.com>
Date: 1998/02/01 Raw View
Hi,
I have the following code entered into my world of Visual C++ 5.0 using
Windows NT 4.0 sp3. It's not quite doing as I expect. The ios::internal
flag, as I read it, will: 'Add fill characters after any leading sign or
base indication, but before the value.' However, this does not seem to be
the case. Using the code below:
#include <iostream>
#include <iomanip.h>
int
main( int ac, char **av )
{
int bNum = -55;
cout << setiosflags( ios::showpos | ios::internal )
<< setfill( '0' )
<< setw( 9 )
<< bNum << endl;
}
I get output that looks like:
000000-55
My fill character is correct, the field width is correct, however the - sign
seems to be in the wrong place. If bNum is positive, it seems to do as
expected; the output is:
+00000055
Any thoughts on why this would be the case or what is happening? Thanks in
advance.
Steve
smann@bridge.com
http://www.interactive.net/~smann/
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Dietmar.Kuehl@IZB-SOFT.de
Date: 1998/02/02 Raw View
Hi,
In article <6asr8e$7of@usenet.bridge.com>,
"Steve Mann" <smann@bridge.com> wrote:
> #include <iostream>
> #include <iomanip.h>
Maybe it is due to mixing old and new style header...?
(it could be a reason but in this specific case, it is not)
> int
> main( int ac, char **av )
> {
> int bNum = -55;
>
> cout << setiosflags( ios::showpos | ios::internal )
> << setfill( '0' )
> << setw( 9 )
> << bNum << endl;
> }
>
> I get output that looks like:
>
> 000000-55
This output is plain wrong. Complain to your vendor about this
misfeature.
> Any thoughts on why this would be the case or what is happening? Thanks in
> advance.
Taking into account who implemented this particular library, I guess the
following is happening: the inserter delegates the processing to the
'num_put'
facet which in turn delegates the processing to some C function. This in
turn is
unfortunately not capable to do internal filling... (just have a look at
the
header xlocnum and you will see that 'num_put<..>::do_put()' delegates
the
processing to 'sprintf()'). IMO, it is inappropriate to implement the
standard
C++ library in terms of the standard C library, mainly because the C++
stuff is
more powerful. OTOH, it is absolutely reasonable to do it vice versa!
--
<email:dietmar.kuehl@claas-solutions.de>
<http://www.informatik.uni-konstanz.de>
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1998/02/03 Raw View
Dietmar.Kuehl@IZB-SOFT.de writes:
|> In article <6asr8e$7of@usenet.bridge.com>,
|> "Steve Mann" <smann@bridge.com> wrote:
|>
|> > #include <iostream>
|> > #include <iomanip.h>
|>
|> Maybe it is due to mixing old and new style header...?
|> (it could be a reason but in this specific case, it is not)
|> > int
|> > main( int ac, char **av )
|> > {
|> > int bNum = -55;
|> >
|> > cout << setiosflags( ios::showpos | ios::internal )
|> > << setfill( '0' )
|> > << setw( 9 )
|> > << bNum << endl;
|> > }
|> >
|> > I get output that looks like:
|> >
|> > 000000-55
|>
|> This output is plain wrong. Complain to your vendor about this
|> misfeature.
In this particular case.
Although not relevant to the standardization issue, it is probably worth
pointing out that you can't generally use the setiosflags manipulator to
set ios::internal; because this manipulator doesn't reset anything, it
can really only be used alone with the boolean flags.
Normally, I would expect to see the above written:
cout << showpoint << internal << ...
To set ios::internal using the setiosflags, you have to use
resetiosflags first, e.g.:
cout << resetiosflags( ios::adjustfield )
<< setiosflags( ios::internal | ios::showpos ).
(Question: since there is a setbase manipulator, is there any reason why
there aren't a setadjust and setfloat manipulators?)
In general, unless the case is truely exceptional, I would suggest
defining an explicit manipulator, something like:
ios_base&
signedDecimalInt( ios_base& stream )
{
stream.flags( ios::dec | ios::showpos | ios::internal ) ;
stream.fill( '0' ) ;
return stream ;
}
// ...
cout << signedDecimalInt << setw( 9 ) << bNum << endl ;
Note that done this way, instead of using setf and resetf, you guarantee
the state of the other bits as well. (General rule: always leave the
formatting state the way you found it, but don't count on it being in
any particular state.) If you want to get really clever, you can
probably work out a way of returning a temporary whose destructor
restores the stream's previous formatting state. (I've gotten into the
habit of always declaring an instance of an iosave class at the top of
any block or function which does formatting, and don't generally worry
about restoring at the level of the individual statement.)
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]