console output formatting

I am sorry, but I need a quick help

what would be analog to this function in C to C++
printf("%5d", var)

cout << var ?????????
but how to format it to take 5 places in console ?
printf("%5d", var) will work in C++. Most C functions, at least all C functions i know, work in C++
you must include <cstdio> and it works printf("%5d", var);
Last edited on
Neither answers his question, however...

To do that in C++, #include <iomanip> and use the following construct:

cout << setw( 5 ) << var;

Hope this helps.
This reluctance to adopt C++ constructs is far too common. It's a pet peeve of mine.
Honestly, I still think printf/etc is easier (as in less typing, easier to remember the codes) than the iostream counterparts. Especially since iostream doesn't seem to remember the last modifiers you give it.

1
2
3
4
5
6
7
8
9
// print 2 numbers 'a' and 'b' in 0xAAAABBBB (hex) format
cout << "0x" << hex << uppercase << setw(4) << setfill('0') << a
             << hex << uppercase << setw(4) << setfill('0') << b << endl;

// vs.
printf("0x%04X%04X\n",a,b);

// or if you want more clarity
printf("0x" "%04X" "%04X" "\n",a,b);


The iostream approach is even more horrifying if you you're not using the "using" directive.


I'm not discounting the merits of iostream. I know that it's better in a lot of ways (and why). I'm just showing why so many people are still reluctant to use it.
Last edited on
I disagree.

The reason people use C stuff over C++ stuff is because they have already been contaminated with C.

The C counterpart doesn't remember the last modifier you give it either.

The only thing that can be said against iostream is that it is more verbose...
Wait, I never learnt C and I still prefer cstdio over iostream in some situations. Terseness is one reason, but the other is that "%08X" is much easier to remember than <<std::setw(8)<<std::setfill('0')<<std::hex. I often find it preferable to just sprintf() to an array than go look up voodoo incantations. Sure, it's laziness, but what exactly am I loosing, other than consistency?
It might be nice to have a manipulator to configure the stream output according to cstdio formatting rules:

 
std::cout << std::cformat("%08X") << ... etc
Duoas wrote:
The C counterpart doesn't remember the last modifier you give it either.


But repeating the format for the C counterpart doesn't involve a full line of text. It's just 4-6 characters tops.

Duaos wrote:
The only thing that can be said against iostream is that it is more verbose...


You say that like it's insignificant. For some things I guess it's not a big deal, but for writing trace logs and stuff where you're dumping lots of data, it's quite a big deal.

Galik wrote:
It might be nice to have a manipulator to configure the stream output according to cstdio formatting rules:


I've thought about that more than once. The thing is it kind of defeats half the point of using iostream.

cstdio's 2 big problems are:
- type safety
- having to parse a format string

By having a cformat() manipulator you're reintroducing half of the problems iostream solves.

Plus, I don't think there's an iostream equivilent of %g, but I might be wrong on that.


Another idea would be to make modifiers for common output formats. Like:

1
2
std::cout << std::c08X << ...
std::cout << std::c04X << ...


But that would require you to write lots of modifiers.
- having to parse a format string
That's not such a big problem.
Alternatively, cformat() could be a compiler construct that requires a string literal and generates proper calls.
Topic archived. No new replies allowed.