Formating output

This is a discussion on Formating output within the C++ Programming forums, part of the General Programming Boards category; I've been using the setw() function to tidy up my output, but it appears that different lengths within the first ...

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    Formating output

    I've been using the setw() function to tidy up my output, but it appears that different lengths within the first value output effect the position of the later ones.

    How do you set the output to it alligns whatever the length.

    Code:
    void print_bill ( BillLine bill_line[50], int num_bill_items )
    {
        cls();
        cout << "THE CORNER SHOP\n\n";
        cout << "Product " << setw(20) << "Kilos" << setw(20) << "Cost()" << endl;
        
        for (int i=0; i<num_bill_items; ++i)
        {
            cout << bill_line[i].stock.desc << setw(20) << bill_line[i].weight 
                 << setw(20) << bill_line[i].cost << endl;
        }
        cls();
    }
    Thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use setw() to set field widths. Make sure widths bigger than items to go into fields as no concatenation will take place. Look up the modifiers left and right to align a bit better.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Still can't get this output correct.

    If, for the first time through the loop, I output a 5 char string for the first variable, then the next output is fine.

    If I output a 15 char string for the first variable, the output for the second is pushed out of line.

    Does anyone have any other guidance please?

  4. #4
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Quick point,

    bill_line[i].stock.desc is a char

    bill_line[i].weight and bill_line[i].cost are both int's

    From what I am reading, this effects the padding, but I am still having trouble getting things to line up

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Wanna try this.
    Code:
    cout << setw(20)  << bill_line[i].stock.desc << setw(20) << bill_line[i].weight 
                 << setw(20) << bill_line[i].cost << endl;
    Just figured this out. This flag will left align the output (instead of right align it). Just use it once at the first cout.
    Code:
    cout << setw(20) << setiosflags(ios::left) << "Product " 
            << setw(20) << "Kilos" 
            << setw(20) << "Cost()" << endl;
    Last edited by alphaoide; 01-14-2004 at 01:34 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

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