News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ TIps:IO - Stream manipulators for oct and hex

Started by ben2ong2, October 06, 2006, 11:08:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Art Freeman)

Error messages:
t.cxx:18: error: In this statement, "ch" is of type "char", and may not be converted to "ios".
t.cxx:19: error: In this statement, "ch" is of type "char", and may not be converted to "ios".
Compilation terminated with errors.


Program listing:
    15      char ch = alpha;
    16      cout << '\'' << ch << '\''
    17           << " = " << int(ch)
    18           << " = 0" << oct(ch)
    19           << " = 0x" << hex(ch) << '\n';


RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage), 8 Feb 93

Your problem is that 'oct' and 'hex' are defined in <iostream.h> as
manipulators:
   ios& oct(ios&);
   ios& hex(ios&);
With no other declarations visible, the compiler must try to convert
the char argument to ios&, which can't be done.

You may be trying to use the old-style (C++ 1.3) formatting functions
   char* oct(long, int=0);
   char* hex(long, int=0);
which are not always supplied with current C++ implementations.

You can get the effect you want like this:
   << oct << int(ch)
   << hex << int(ch)

Bear in mind that the conversion base of the stream is changed by
the manipulators and remains until explicitly changed again.  You
could add
   cout << dec;
to restore the decimal base, or use the setf() functions.


You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login