Thread: setw

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    setw

    When using setw with cout, how do i center align the text/numbers within the specified width?


    Thanks

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    There’s no specified stream manipulator that does that, you need to calculate the centre column coordinate yourself, then call gotoxy().
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    subdene's suggestion is good, but remember that gotoxy() is non-standard and non-portable.

    If that isn't an issue - should always be, but let's deal with that one later - use subdene's idea.

    (Off-hand, I don't know of a better way. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Is this what you wanted?

    When using setw with cout, how do i center align the text/numbers within the specified width?
    It would be something like so...

    cout << setfill('*') << left //left justifies
    << setfill('$') <<right //right justifies
    << setw(6) << endl; // set width to 6 places

    I hope this was want your looking for.
    Later
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square ints, setw problem(not too hard ;))
    By Daniel Primed in forum C++ Programming
    Replies: 9
    Last Post: 01-19-2006, 03:25 PM
  2. setw question
    By dhrodrigues in forum C++ Programming
    Replies: 9
    Last Post: 07-14-2004, 08:34 AM
  3. setw newb help
    By smcan in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2003, 07:27 PM
  4. Problem with setw
    By PJYelton in forum C++ Programming
    Replies: 3
    Last Post: 10-15-2002, 02:56 PM
  5. setw() and named constants
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 02-11-2002, 02:22 PM