Still can't align columns, is this really that hard?

Discussion in 'Mac Programming' started by cybrscot, Jan 4, 2011.

  1. cybrscot, Jan 4, 2011
    Last edited by a moderator: Jan 4, 2011

    cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    I still can't line up vertical columns, code below, as one person suggested I use the space bar instead of tabs, I did that. I erased my code that looked just like this, but had used tabs to do it., then I rewrote it and aligned Unit and price, and Purchase and Date with the space bar. But the output is exactly the same as before. Is this really just trial and error trying to make a vertical column? there is no way to do it, like with placeholders (left align, right align, etc?? ) Since the output is aligning about 4 or 5 spaces to the left, do I just need to move it in my code 4 or 5 spaces to the right? So in the code it won't look right, but the program will? No way to make them both the same?

    Code:
    printf ("Item:		Unit		Purchase\n" );
    	 printf ("                    Price          Date\n" ) ;	
    output for running program...

    Item: Unit Purchase
    Price Date

    I just did it, but I had to keep playing with the spaces until the output looked right, is this the right way to program? This is what I had to leave it at to get the proper output..... crazy! okay, in the edit screen on here and while posting, my code looks as I intended. But I just looked at my post after posting it, and the code tags ruin the integrity of what I just posted, moving everything around. what a pain!! So below I will try again, the first one without code tags, and the 2nd with. Okay, now I see they both look the same on the finished post, with our without code tags. So I guess I cannot post my code the way I wrote it, there is no way to show you the spacing and alignment that I used. I can just say that it was a very unscientific approach, just trial and error on placement, until the output was correct below. Oh, I also see now that even my copy/paste from terminal doesn't look right!!!!!!! I can assure you that when I wrote it, it was aligned properly, and when I hit the post button it was perfect, but between posting and reading the post, MR somehow changes it and it doesn't stay as I wrote it!!! What a pain in the tooth!!!!


    Code:
    printf ("Item:		Unit		Purchase\n" );
    	printf ("                Price           Date\n" ) ;
    
    printf ("Item:		Unit		Purchase\n" );
    	  printf ("                           Price             Date\n" ) ;

    program output from terminal

    Code:
    Enter purchase date (mm/dd/yy): 11/09/10
    Item:		Unit		Purchase
                    Price          Date
     
  2. balamw, Jan 4, 2011
    Last edited: Jan 4, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Please fix your CODE tags, you are missing a "[" in the closing tag so it doesn't format right. As posted, I would not guess your code would align properly.

    If all you have is C and printf/scanf then yes, this is how it is done. "Real" formatting is better done using third party tools and libraries.

    e.g. if your goal is formatted printed output you could output TeX or PostScript as a text file and process/print that. If you want it on the screen use Cocoa.

    EDIT: Are you using the "#" pound/hash button in the edit window or entering the CODE tags yourself? The easiest way to enter code is to hit the "#" then paste between the tags. The program output also needs to be within the tags if you want any hope of aligning things.

    EDIT 2: Alignment of strings isn't hard, just line up the opening double quotes and use spaces where necessary.

    B
     
  3. cybrscot, Jan 4, 2011
    Last edited by a moderator: Jan 4, 2011

    cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    Hmmm...I see that missing bracket you are talking about. Strange both my posts are missing only that, and yes I just copy and pasted between the two CODE words, didn't type them myself. I don't know why it disappeared. But thanks for pointing that out as I didn't notice it, I can make sure next time it's correct.

    Ahhh, I just found a better way to align the columns than have two printf strings and manually play with spacing.. it's the following code.....


    Code:
    printf ("Item\tUnit\tPurchase\n\tPrice\tDate\n") ;
    it came out perfect in the terminal, much easier!!
     
  4. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #4
    One more thing to check is that you're using fixed width fonts in both the editor where you type your code and the terminal window where your code is running.

    With variable width fonts, the calculations get a lot more complicated to line things up.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Beware an item you're printing being too long to fit in one tabstop.

    -Lee
     
  6. balamw, Jan 4, 2011
    Last edited: Jan 4, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    Thanks for finally getting the CODE tags right.

    Better is relative. You still have no guarantees how the tab stops are set up if you switch anything. If I have tab stops set to 4 characters instead of 8 your code will no longer line up because Price is longer than 4 characters.

    e.g. http://www.mackb.com/Uwe/Forum.aspx/mac/4700/Q-Tab-spacing-in-Terminal-app

    EDIT:
    If you're going to use tabs and distribute your code you need to make sure you control the tab stops. See attachment for example.


    B
     

    Attached Files:

  7. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #7
    I just checked my editor, (textwrangler) and I'm using fixed width fonts (monaco) and my tab stops are set at 4.

    However I don't know how to choose fixed width fonts in my terminal. How?
     
  8. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #8
    If you don't know then that's good as it means you haven't changed it and it'll be fixed width by default.

    Terminal Preferences -> Settings -> Text tab -> font
    That's in the one that comes with Snow Leopard, it used to be different.
     
  9. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    That explains why your original experiment with literal tabs (^I not \t) didn't work. Terminal defaults to tab stops of 8.

    NOTE: As most code editors do, TextWrangler has an option to auto expand tabs into spaces. I'd suggest you use it.

    B
     
  10. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    Don't use tabs at all, use the field width in printf.

    Code:
    printf("%-10s%-10s%-10s\n%-10s%-10s\n", "Item", "Unit", "Purchase", "Price", "Date");
    Note the - in front of the width, it makes the output left justified, which is what you want in this case.
     
  11. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #11
    You guys are awesome! A lot of great suggestions, good explanations, I understand a lot more now about how to do it. Now I know also why my literal tabs were not coming out the same, because terminal and my editor are different, so they were displayed by terminal the way it does tabs. I get it. I like the suggestion of the field width string, I was wondering how to do that with printf words, rather than values like %d, or %f.

    Thanks! I finished my exercises, now on to Chapter 4.
     
  12. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    I don't think you can, not that I'm aware of at least. But since this is for a table, that format will make more sense when you print the table (in some kind of loop).

    If you wanted to you could create a pre formatted string just for the header as well.

    Code:
    const char *tableheader = "Item      Unit      Purchase\n          Price     Date\n";
    puts(tableheader);
     
  13. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #13
    As I was reading down I was thinking exactly the same thing. There are A LOT of ways printf can help formatting.

    Another example is using an integer so you can calculate how wide things need to be and supply the width at run time.

    Code:
    	int size = 10;
    	printf("%-*s%-*s%-*s\n%-*s%-*s\n", size, "Item", size, "Unit", size, "Purchase", size, "Price", size, "Date");
    	printf("\n\n");
    	size = 20;
    	printf("%-*s%-*s%-*s\n%-*s%-*s\n", size, "Item", size, "Unit", size, "Purchase", size, "Price", size, "Date");
    
     
  14. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    Definitely lots of ways to skin these cats.

    It's also worth remembering that in real life, in our GUI age, you probably would not be using printf to display this to a terminal/fixed width display. You'd be far more likely to do something different like use the widgets and controls provided by the UI of your OS or a library or, if you are really wanting flexible tabular output, drive your favorite spreadsheet program to do it for you.

    B
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    There's also the ncurses library. Command-line tools like 'top', 'pico', and 'vi' use it.
     
  16. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #16
    That's what I was thinking of earlier and lumping in to "libraries" but I couldn't remember what it was called. ;) Thanks!

    B
     
  17. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #17
    Most real life command line tools does this.
     
  18. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #18
    Yes, and no.

    And I'm not just referring to using libraries like ncurses where formatting/ interactive control of the screen is important.

    e.g. many of Apple's own command line tools will output a plist instead of screen formatted text for ease of parsing by GUI tools. For example:
    Code:
    diskutil list -plist
    I've written plenty of command line code that will output to the screen in a CSV format, intended to be interpreted by another program like a spreadsheet. Other tools may output HTML or XML to be interpreted by a browser.

    B
     
  19. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #19
    I said 'most' not all, you get a plist output since you specify the plist flag not surprisingly.
    Code:
    diskutil info /dev/disk0
    Since the thread is about printf and c specifically it's not out of place to talk about printf formatting, it IS still used in these circumstances.
     
  20. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #20
    I'm sure that at some level printf is used even to generate the plist, but it is accessed by the code as part of a higher level Objective-C function.

    My point is that cybrscot ultimately wants develop in Cocoa, and there many of the screen formatting aspects of printf (vestiges of punchcards and teletypes) that we are focusing on in this thread will go away and be replaced by some completely other (hopefully simpler and higher level) interface.

    As I mentioned earlier in the thread, if you're trying to build a report (invoice, receipt, purchase order, etc..) of the kind that cybrscot is working on that is intended for printing or even viewing on the screen today you would likely use some other way to format it (TeX, PostScript, PDF, HTML, XML, RTF, ...) that would give you more options and flexibility.

    B
     
  21. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #21
    Probably, but I was referring more to the column formatted output to stdout.

    I see your point, and I agree. Except the part that it will go away, simply use the tool appropriate for what's the given task. For end user desktop GUI applications printf is completely irrelevant.
     
  22. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #22
    My point exactly. For cybrscots long term goal this is the wrong place to be concentrating tons of effort.

    It's like learning how to use an abacus, certainly useful but how often will you use it over the alternatives?

    B
     
  23. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #23
    If anything this teaches how to use documentation and your imagination to solve a problem. I think you do that continuously as a programmer.

    EDIT: oh and add some commas to my above example and you get something that can be parsed as easily by a computer as by your own eyes.
     

Share This Page