# C++ gurus?  (output formatting help)

**URL:** <https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966>\
**Category:** Factual Questions\
**Created:** [February 1, 2004, 12:35am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966 "2004-02-01T00:35:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerodave](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aerodave](https://boards.straightdope.com/u/aerodave)\
**Post date:** [February 1, 2004, 12:35am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/1 "2004-02-01T00:35:57Z")

</div>

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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![leenmi](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@leenmi](https://boards.straightdope.com/u/leenmi)\
**Post date:** [February 1, 2004, 1:23am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/2 "2004-02-01T01:23:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![np\_complete](https://avatars.discourse-cdn.com/v4/letter/n/ecb155/32.png) [@np\_complete](https://boards.straightdope.com/u/np_complete)\
**Post date:** [February 1, 2004, 2:10am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/3 "2004-02-01T02:10:36Z")

</div>

You’ll want use one of

```auto

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](http://msdn.microsoft.com)) for [ios\_base::internal](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/IOS_ios_baseCCfmtflags.asp).

---

<div class="post-metadata">

**Author:** ![Achernar](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Achernar](https://boards.straightdope.com/u/Achernar)\
**Post date:** [February 1, 2004, 2:37am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/4 "2004-02-01T02:37:15Z")

</div>

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”.

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Algorithm](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/algorithm/32/12204_2.png) [@Algorithm](https://boards.straightdope.com/u/Algorithm)\
**Post date:** [February 1, 2004, 2:46am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/5 "2004-02-01T02:46:56Z")

</div>

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):

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Achernar](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Achernar](https://boards.straightdope.com/u/Achernar)\
**Post date:** [February 1, 2004, 3:01am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/6 "2004-02-01T03:01:43Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![aerodave](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aerodave](https://boards.straightdope.com/u/aerodave)\
**Post date:** [February 1, 2004, 3:51am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/7 "2004-02-01T03:51:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aerodave](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aerodave](https://boards.straightdope.com/u/aerodave)\
**Post date:** [February 1, 2004, 3:59am UTC](https://boards.straightdope.com/t/c-gurus-output-formatting-help/226966/8 "2004-02-01T03:59:48Z")

</div>

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

It’s described in the [MS Knowledge Base](http://support.microsoft.com/default.aspx?scid=kb;EN-US;123161). Says it only affects negative integers and not positive ones.

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

```auto

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 😃

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