C++ gurus? (output formatting help)

I’m trying to write a simple C++ program to do some iterative and tedious math. It’s coming along well, and now I’m to the point where I’m getting fussy about formatting.

The problem is, I want a number to be output with its sign ahead of a number in a fixed length field, filled with leading zeros where necessary. I may want to use a different character later, so I’m trying to make the setfill IO manipulator work for me.

So far, I’ve got:



int answer;

...

cout << setfill ('0') << setw (8) << showpos;
cout << answer << endl;


My problem is that I can’t get the sign to stay before the field. If I have an integer like -1246, I want it do display as -00001246. And I want 21597 to display as +00021597. But in the above code, the answer displays as 000-1246

Is there any way to do this simply with io flags? I don’t want to have to make some sort of stupid condtional statement just to format my numbers this way.

Not a C or C++ programmer, but I thought printf or sprintf were used to format output, including padding with leading digits.

You’ll want use one of



std::cout.setf( std::ios_base::internal );
std::cout << std::setiosflags( std::ios_base::internal );
std::cout << std::internal;

Here’s the doc page (courtesy MSDN) for ios_base::internal.

I agree with np_complete. The following works in my compiler, and I think it’s ANSI, but it looks like you have some things that I don’t, like “setfill”.


int answer;
cout.flags(ios::showpos | ios::internal);
cout.fill('0'); cout.width(8);
cout << answer << endl;

The setfill( ) function inserted into an output stream via cout has the same effect as executing the cout.fill( ) function separately. The following code will have the same output as Achernar’s (ignoring the fact that answer has no value set to it):


int answer;
cout << setiosflags( ios::showpos | ios::internal )
     << setfill('0') << setw(8) << answer << endl;

Aha I see what I was doing wrong. I just needed to include iomanip, something I’d never heard of. Ugh, I’ve been doing it the long way all these years!

Well, that works great for positive numbers. But not for negative ones. Feel free to try it and let me know if I’m missing something.

And the “internal” mask definitely does something. Without it, if answer is 471, I get 0000+471. WIth it, I get +0000471.

But when answer is -471, I get 0000-471 regardless of whether the internal modifier is in there. I’m confused.

FWIW, I’m using MS Visual C++ 6 Standard.

Wait…just figured it out…it’s a bug in Visual C++.

It’s described in the MS Knowledge Base. Says it only affects negative integers and not positive ones.

So, I did the following, and it works like a charm.




float answer;

cout << setiosflags( ios::showpos | ios::internal | ios::fixed) << setfill('0') << setw(8) << setprecision(0) << answer << endl;



'Tain’t elegant, but it works. And that’s all that matters to me. I’m no programmer :smiley:

But I couldn’t have done it without you guys. You pointed me in the direction of the internal flag.