# C++ Formatting

**URL:** <https://boards.straightdope.com/t/c-formatting/85464>\
**Category:** Factual Questions\
**Created:** [October 4, 2001, 11:21pm UTC](https://boards.straightdope.com/t/c-formatting/85464 "2001-10-04T23:21:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hightechburrito](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hightechburrito](https://boards.straightdope.com/u/hightechburrito)\
**Post date:** [October 4, 2001, 11:21pm UTC](https://boards.straightdope.com/t/c-formatting/85464/1 "2001-10-04T23:21:28Z")

</div>

I am working on a problem where I need centered output for a C++ program. Is there an easy way that I can do this?

And if there is, I just might shoot myself.

---

<div class="post-metadata">

**Author:** ![ModernRonin2](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@ModernRonin2](https://boards.straightdope.com/u/ModernRonin2)\
**Post date:** [October 4, 2001, 11:34pm UTC](https://boards.straightdope.com/t/c-formatting/85464/2 "2001-10-04T23:34:56Z")

</div>

Generally, if you’re on a standard 80x25 text screen,  
you can just use the following algorithm:

- Count the number of characters in your string. (strlen() or string.length or whatever)
- Subtract that number from 80
- Divide by 2
- Pad the string on the left side with that may spaces

This will center a text string on a standard “console”/DOS style 80x25 screen.

If you’re talking about fonts on a graphical screen, it gets more complicated. You have to know the width of the  
viewport in pixels, and the width of the string in pixels, etc…  
-Ben

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 4, 2001, 11:38pm UTC](https://boards.straightdope.com/t/c-formatting/85464/3 "2001-10-04T23:38:48Z")

</div>

I don’t know of anything off the top of my head. Check the iomanip library.

---

<div class="post-metadata">

**Author:** ![hightechburrito](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hightechburrito](https://boards.straightdope.com/u/hightechburrito)\
**Post date:** [October 4, 2001, 11:41pm UTC](https://boards.straightdope.com/t/c-formatting/85464/4 "2001-10-04T23:41:17Z")

</div>

The program takes in an integer and a character. It needs to print out a diamond the width of the int and made up of the char. So if I enter 5 for int and \* for char, it _should_ look like this:

–\*  
-\*\*\*

* * *

-\*\*\*  
–\*

but mine looks like this:  
\*

* * *

* * *

* * *

- 

So the length before each line is different for each line and different for other ints that could be enterred, so I can’t just pad the space before it.

---

<div class="post-metadata">

**Author:** ![smoke](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@smoke](https://boards.straightdope.com/u/smoke)\
**Post date:** [October 5, 2001, 12:23am UTC](https://boards.straightdope.com/t/c-formatting/85464/5 "2001-10-05T00:23:59Z")

</div>

Could you pad the margins by creating a for loop that does the math for you and inserts hard-coded spaces based on the size of your integer?

Is that at all clear?

-j

---

<div class="post-metadata">

**Author:** ![Flymaster](https://avatars.discourse-cdn.com/v4/letter/f/c4cdca/32.png) [@Flymaster](https://boards.straightdope.com/u/Flymaster)\
**Post date:** [October 5, 2001, 12:28am UTC](https://boards.straightdope.com/t/c-formatting/85464/6 "2001-10-05T00:28:09Z")

</div>

It’s definetly in \<iomanip\>. I forget the exact syntax, but it’s in there somewhere.

---

<div class="post-metadata">

**Author:** ![Manduck](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/manduck/32/256_2.png) [@Manduck](https://boards.straightdope.com/u/Manduck)\
**Post date:** [October 5, 2001, 12:28am UTC](https://boards.straightdope.com/t/c-formatting/85464/7 "2001-10-05T00:28:26Z")

</div>

Should we really be doing this guy’s homework for him?

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 5, 2001, 1:05am UTC](https://boards.straightdope.com/t/c-formatting/85464/8 "2001-10-05T01:05:41Z")

</div>

> [@](#):
>
> \*Originally posted by hightechburrito \*  
> So the length before each line is different for each line and different for other ints that could be enterred, so I can’t just pad the space before it.

I don’t see what the problem is here. Why can’t you just do **Ronin** ’s algorithm for each line?

---

<div class="post-metadata">

**Author:** ![Ell](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ell](https://boards.straightdope.com/u/Ell)\
**Post date:** [October 5, 2001, 1:31am UTC](https://boards.straightdope.com/t/c-formatting/85464/9 "2001-10-05T01:31:43Z")

</div>

How about using the setw function (in iomanip)?
