Aligning text

This is a discussion on Aligning text within the C++ Programming forums, part of the General Programming Boards category; Okay so I should know this (and I know I did at one time) but: I want to print text ...

  1. #1
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15

    Aligning text

    Okay so I should know this (and I know I did at one time) but:

    I want to print text to the screen like this: (the underlines are spaces, only way I could get the spacing right)

    5_____6 _____3
    10____2______1

    but using setw(x) it looks like this:

    5_____6______3
    10_____2______1

    using setw the amount of spaces always starts from the last character. I'm assuming that setw isn't the correct command, so what am I missing here?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    setw works kinda like a tab... if you setwidth to 5, the last character of the next output will be put in the 5th space... if there's too many characters (more than 5) it will just put it directly next to the last thing...

    Code:
    ...
    cout<<hello<<setw(10)<<world;
    ...
    output: "hello.....world"
    Code:
    ...
    cout<<hello<<setw(3)<<world;
    ...
    output: "helloworld"

    all of the '.' in the first output are spaces...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >using setw the amount of spaces always starts from the last character
    This works for me on multiple compilers (G++, VC++, DevC++, Borland C++):
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        const int i = 5;
    
        cout<< left << setw(i) << 5 << setw(i) << 6 << setw(i) << 3 <<endl;
        cout<< left << setw(i) << 10 << setw(i) << 2 << setw(i) << 1 <<endl;
    }
    Code:
    Output:
    
    5     6     3
    10    2     1
    My best code is written with the delete key.

  4. #4
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15

    Smile

    Thanks Prelude that works great.

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Why not put setw(5) instead of setw(i).

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why not put setw(5) instead of setw(i).
    What if you decide to use something else instead of 5?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21