The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++ output formatting

RainOPainRainOPain Registered User regular
edited May 2008 in Help / Advice Forum
If I use the setw() function to format output in C++, it puts the text in a column of set width and then right justifies it. Is there some way to use a similar function that will center justify the text? Essentially, I have to add a functon to this binary search tree that prints out the tree in textual format like this:
[PHP] 10
5 12
4 6 11 13[/PHP]

But the way I currently have it, it looks all wacky and the numbers don't line up. I think center justifying the text instead of right justifying it might help

Anyone?

RainOPain on

Posts

  • VThornheartVThornheart Registered User regular
    edited May 2008
    I don't think there's a way to formally "center justify" in a console window that I know of. You can pull it off mathematically however.

    For instance, if you know that the terminal is 80 characters wide, you can determine that the center position is 40 (or 39, I forget how it goes given the fact that you'll be using up a character).

    So for each character you have to type on the line, do something like this:

    Spaces_To_Put_Before = (Total width of page / 2) - (Total width of characters you want to type on line / 2)

    Then just prepend that console output with that calculated # of spaces. It'll come out fairly well centered.

    VThornheart on
    3DS Friend Code: 1950-8938-9095
  • exisexis Registered User regular
    edited May 2008
    If you can't get the answer you're looking for here, try this thread.

    exis on
  • SmasherSmasher Starting to get dizzy Registered User regular
    edited May 2008
    On each successive line the distance between adjacent characters roughly halves itself (barring factors such as multi-digit numbers). Likewise, on each line the distance from the beginning of the line to the first character also halves itself. The latter is easy to calculate for the first line, as it's just (width of space/2). The former seems a bit trickier; since there's only one number on the first line, it doesn't really make much sense to talk about the distance between numbers on that line. However, on the second line we want the two numbers to be 1/4 and 3/4 of the way across the space respectively, which is half the width of the space. So, on the first line the (imaginary) distance is the whole width of the space.

    To put it in more concrete terms, let's say our space is 80 characters wide. The first line starts at space 40 and has 80 spaces between numbers. The second line starts at 20 and has 40 spaces between numbers. The third starts at 10 and has 20 spaces, etc. Also, you should take into account the number of digits in the numbers you're printing and adjust the widths you specify accordingly.

    Smasher on
Sign In or Register to comment.