C++ Formatting

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.

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

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

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.

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

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

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

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

How about using the setw function (in iomanip)?