Problem in setting I/O format strings in DEV c/c++

  • Thread starter Nikos Hatzigiannakis
  • Start date
N

Nikos Hatzigiannakis

The following code does not display the number 10 in hex format. The hex
flag doesn't seem to work.

Any ideas?



Compiler is DEV C++ 4.9.9.2 http://www.bloodshed.net/



#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;


int main ()
{
int a=10;
cout.setf(ios::hex);
cout<<a;
cout<<setiosflags(ios::hex)<<a<<endl;

system("pause");
}
 
J

Jim Langston

Nikos Hatzigiannakis said:
The following code does not display the number 10 in hex format. The hex
flag doesn't seem to work.

Any ideas?

Compiler is DEV C++ 4.9.9.2 http://www.bloodshed.net/



#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;


int main ()
{
int a=10;
cout.setf(ios::hex);
cout<<a;
cout<<setiosflags(ios::hex)<<a<<endl;

system("pause");
}

try
std::cout.setf(std::ios::hex, std::ios::basefield);
and no, I don't know what basefield is or why it works that way, I just
found that out by googling.
 
P

Pete Becker

try
std::cout.setf(std::ios::hex, std::ios::basefield);
and no, I don't know what basefield is or why it works that way, I just
found that out by googling.

basefield is the mask for the bits that determine the base to use for
I/O. If you just setf(ios::hex) you jam in whatever value ios::hex has,
and might end up with an invalid flag setting. When you use
setf(ios::hex, ios::basefield) you clear the bits represented by
basefield, then jam in the value of ios::hex.

In general, you need to do the analogous thing with all of the flags
that have more than two values.
 
B

BobR

Jim Langston wrote in message...
in message...

try
std::cout.setf(std::ios::hex, std::ios::basefield);
and no, I don't know what basefield is or why it works that way, I just
found that out by googling.

"
Method: fmtflags ios::setf (fmtflags flag, fmtflags mask)
Clear the flag values indicated by mask, then set any of them that are also
in flag. (Flag values are described for `ios::flags ()'.) Return the
complete collection of flags previously in effect. (See ios::unsetf for
another way of clearing flags.)
"

So, it clears the 'basefield', then sets it to 'hex'.

OP:

int main (){
int a=10;

std::cout<<std::hex<<"0x"<<a<<std::endl;

system("pause");
}
 
J

James Kanze

basefield is the mask for the bits that determine the base to use for
I/O. If you just setf(ios::hex) you jam in whatever value ios::hex has,
and might end up with an invalid flag setting. When you use
setf(ios::hex, ios::basefield) you clear the bits represented by
basefield, then jam in the value of ios::hex.
In general, you need to do the analogous thing with all of the flags
that have more than two values.

You mean, with all of the flags which aren't flags:). And of
course, std::ios::hex only has one value, at least in a given
implementation.

The way I think of it is that fmtflags is conceptually a struct.
Some of the fields are boolean (flags), and can be set and reset
using the single argument versions of setf and unsetf. There is
no separate mask value (i.e. no ios::showposmask) for these,
since the set value is the mask (supposing you needed a mask).
Others, such as the base, the floating point representation, and
the alignment, are conceptually enum's: these have a distinct
mask value (field name), and can only be manipulated using the
two argument form of setf, with the name of the field as the
second argument.

Note that in the two argument form, you must specify all of the
affected fields in the second argument. Just saying:
setf( ios::showpos | ios::hex, ios::basefied )
isn't sufficient---you have to say:
setf( ios::showpos | ios::hex, ios::showpos | ios::basefield)
IMHO, however, it's clearer if you use two separate function
calls for this.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top