Manipulator question...
  Home FAQ Contact Sign in
comp.lang.c++ only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++ Profile…
 Up
Manipulator question...         


Author: barcaroller
Date: May 23, 2007 14:33

When I use an iostream manipulator in a cout statement, it seems to affect
all subsequent cout statements.

cout << x << endl; // dec by default
cout << hex << x << endl; // hex
cout << x << endl // still hex

Is there a way I can have manipulators affect only the "current" cout
statement?
4 Comments
Re: Manipulator question...         


Author: Pete Becker
Date: May 23, 2007 14:37

barcaroller wrote:
> When I use an iostream manipulator in a cout statement, it seems to affect
> all subsequent cout statements.
>
> cout << x << endl; // dec by default
> cout << hex << x << endl; // hex
> cout << x << endl // still hex
>
> Is there a way I can have manipulators affect only the "current" cout
> statement?
>

Save the stream's formatting flags, manipulate it, and when you're done,
restore the flags. For that you'll need to read about the
ios_base::flags functions.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
no comments
Re: Manipulator question...         


Author: BobR
Date: May 24, 2007 10:21

barcaroller music.net> wrote in message...
>
> When I use an iostream manipulator in a cout statement, it seems to affect
> all subsequent cout statements.
>
> cout << x << endl; // dec by default
> cout << hex << x << endl; // hex
> cout << x << endl // still hex
>
> Is there a way I can have manipulators affect only the "current" cout
> statement?

What Pete said.
But, for a few dollars more, there is another way:
Show full article (1.42Kb)
no comments
Re: Manipulator question...         


Author: sebor
Date: May 25, 2007 15:18

On May 23, 3:33 pm, "barcaroller" music.net> wrote:
> When I use an iostream manipulator in a cout statement, it seems to affect
> all subsequent cout statements.
>
> cout << x << endl; // dec by default
> cout << hex << x << endl; // hex
> cout << x << endl // still hex
>
> Is there a way I can have manipulators affect only the "current" cout
> statement?

Not without writing a bit of code. Here's a simple example
implementation
of such a manipulator in the Apache C++ Standard Library:
http://svn.apache.org/repos/asf/incubator/stdcxx/trunk/examples/manual/fmtflags_manip...
Show full article (0.91Kb)
no comments
Re: Manipulator question...         


Author: sebor
Date: May 31, 2007 19:08

On May 23, 3:33 pm, "barcaroller" music.net> wrote:
> When I use an iostream manipulator in a cout statement, it seems to affect
> all subsequent cout statements.
>
> cout << x << endl; // dec by default
> cout << hex << x << endl; // hex
> cout << x << endl // still hex
>
> Is there a way I can have manipulators affect only the "current" cout
> statement?

Not without writing a bit of code. Here's a simple example
implementation
of such a manipulator in the Apache C++ Standard Library:
http://svn.apache.org/repos/asf/incubator/stdcxx/trunk/examples/manual/fmtflags_manip...
Show full article (0.91Kb)
no comments