Thread: problem formatting

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    problem formatting

    I have a program that prints out inventory which is formatted so that its lined up on the right like so:
    Code:
           Strawberries
            Watermelons
                Bananas
    using the following code:
    Code:
    for (iter=items.begin(); iter!=items.end(); iter++)
        cout<<setw(30)<<right<<(*iter)<<endl;
    What I want to do now is to add a grid reference to where the items are. The grid reference is to be written like (x,y) and I want to put it in front of the item when printing the list, but I want to keep the list lined up on the right 30 spaces over, but obviously:
    Code:
    for (iter=items.begin(); iter!=items.end(); iter++)
        cout<<setw(30)<<right<<"("<<iter->x<<","<<iter->y<<") "<<(*iter)<<endl;
    will just line up the "(" 30 spaces over. Is there an easy way to do this?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I tried that code, and it seems to work except "right", which I've never heard of. If there is a problem, it may be in your items class.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    easy no, impossible no. You will need to use a different set of flags/manipulators for each field. I don't have my reference with me but it might look something like this, where the dots and underscores are just space indicators for a given field

    (1, 3)........._____watermelon
    (2, 6).........________bananas

    string coordinates = "(1, 3)";
    string name = "watermelon";

    cout.width(15);
    cout << setf(ios::left) << corrdintaes;
    cout.width(25);
    cout << setf(ios::right) << name << cout << endl;

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Oops, looks like I wasn't very clear. What I meant was to print out like:
    Code:
          (1,10) Strawberries
            (3,4) Watermelons
               (10,0) Bananas
    I believe right and left are from the iomanip header, and it's the same as setf(ios::right), although I'm not sure if right and left work outside of MSVC++...
    Last edited by PJYelton; 11-07-2002 at 11:49 AM.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Nope. 'right' and 'left' work the same with Borland, as well.

    The problem is that you must use setw() between each insertion operator. Convenient, eh?

    For lack of a better way to put this, you may, very well, be stuck with the "brute force" method.

    One possibility may be to "left-align" your coordinates while "right" aligning your fruit...if you'll pardon the expression. It may not give you the "cosmetics" you're looking for, but sometimes you have to kick a program in the teeth!

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

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks for the help! The only other thing I can think of is to try and convert it all into one string through the use of a function, but that may be more trouble than its worth...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Formatting problem with dates.
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2002, 09:35 AM