Problem using I/O manipulators

P

Paul Davis

I have some simple test code, which is copied pretty much verbatim
from Josuttis (although he doesn't give complete examples):

#include <iostream>
main() {
cout << hex << 1 << endl;
std::cout << std::ios::hex << 1 << std::endl;
}

That's the whole program. The first line produces the correct output
('1'), but the second line produces '401'. Can anyone tell me what's
going on here? This is on gcc 3.3.

Many thanks

Paul
______________________________________
To get a valid mail address: s/m@/m1@/
 
M

Mike Wahler

Paul Davis said:
I have some simple test code, which is copied pretty much verbatim
from Josuttis (although he doesn't give complete examples):

#include <iostream>
main() {
cout << hex << 1 << endl;
std::cout << std::ios::hex << 1 << std::endl;
}

That's the whole program. The first line produces the correct output
('1'), but the second line produces '401'. Can anyone tell me what's
going on here? This is on gcc 3.3.

std::ios::hex is a value (of type std::ios_base::fmtflags),
it's not a manipulator. So when you "print" it, you get
its value (which may be different among standard library
implementations). 'std::hex' is a manipulator, which uses
the value std::ios::hex to 'manipulate' the output
formatting.

This is explained in the Josuttis book.

-Mike
 
R

Rolf Magnus

Paul said:
I have some simple test code, which is copied pretty much verbatim
from Josuttis (although he doesn't give complete examples):

#include <iostream>
main() {
cout << hex << 1 << endl;
std::cout << std::ios::hex << 1 << std::endl;
}

That's the whole program. The first line produces the correct output
('1'), but the second line produces '401'. Can anyone tell me what's
going on here? This is on gcc 3.3.

I doubt that. The above program doesn't compile on gcc 3.3. Anyway,
std::ios::hex is not the same as std::hex. The former is a flag, the
latter a function.
 
P

Paul Davis

Paul Davis wrote:

<snipped>
I doubt that. The above program doesn't compile on gcc 3.3. Anyway,
std::ios::hex is not the same as std::hex. The former is a flag, the
latter a function.

Whoops.. my fault. My g++ points to 2.96. This is the 3.3 version:

#include <iostream>
main() {
std::cout << std::hex << 1 << std::endl;
std::cout << std::ios::hex << 1 << std::endl;
}

std::hex works fine, std::ios::hex doesn't.

Paul
 
M

Mike Wahler

Paul Davis said:
Whoops.. my fault. My g++ points to 2.96. This is the 3.3 version:

#include <iostream>
main() {
std::cout << std::hex << 1 << std::endl;
std::cout << std::ios::hex << 1 << std::endl;
}

std::hex works fine, std::ios::hex doesn't.

They both 'work fine'. The second output simply
does not do what you (mistakenly) expect.

Read Rolf's reply again, and mine.

-Mike
 
P

Paul Davis

std::ios::hex is a value (of type std::ios_base::fmtflags),
it's not a manipulator. So when you "print" it, you get
its value (which may be different among standard library
implementations). 'std::hex' is a manipulator, which uses
the value std::ios::hex to 'manipulate' the output
formatting.

This is explained in the Josuttis book.

-Mike

Hmmm. Quoting verbatim from p622:
For example, the following statements write x and y in hexadecimal, and z in decimal:
int x,y,z;
...
std::cout << std::ios::hex << x << std::endl;
std::cout << y << ' ' << std::ios::dec << z << std::endl;

There's an example further down the page where he uses 'std::hex'
instead. Looks like I'm not the only one who got confused.. :)

Do you happen to know if there's a better reference for formatting?
I'm trying to implement a subset of printf, and there dont seem to be
any manipulators or flags to set an integer precision, for example.

Thanks

Paul
 
M

Mike Wahler

Paul Davis said:
Hmmm. Quoting verbatim from p622:


There's an example further down the page where he uses 'std::hex'
instead. Looks like I'm not the only one who got confused.. :)

Alas, people are not perfect. :)

Visit http://www.josuttis.com/libbook/
Click on the "Errata" link, and search for "622"

You might want to peruse the other errata items as well.
Do you happen to know if there's a better reference for formatting?

The material in the Josuttis book is imo enough for
'basic' use of i/o formatting, but there is another
excellent book which has a much "deeper" treatment
of all things IOStream:

http://www.langer.camelot.de/iostreams.htm
I'm trying to implement a subset of printf, and there dont seem to be
any manipulators or flags to set an integer precision, for example.

What do you mean by "integer precision"? "Precision"
only applies to floating point values.

-Mike
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top