Columns not lining up

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

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

    cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    Hello, I can't line up my columns in my code. The code looks okay, and is lined up, but the output is not lined up. What can I do to fix it?

    As you can see in my code, unit price and purchase date are lined up vertically. But they don't come out okay in the execution of the program.

    Thanks! I'm not sure if I'm supposed to use m for minimum field width or integers with - symbols? If so, how do I do it with my code? I used tab stops in writing my code, which looks perfect on the code, but not when the progra is run. Thanks

    Code:
    #include <stdio.h>
    
    main ( )
    
    {
    
    	int item ;
    	float price ;
    	int month ;
    	int day ;
    	int year ;
    	
    	
    	
    	printf ("Enter item number: ") ;
    	scanf ("%d", &item ) ;
    	printf ("Enter unit price: ") ;
    	scanf ("%f", &price) ;
    	printf ("Enter purchase date (mm/dd/yy): ") ;
    	scanf ("%d/%02d/%d", &month, &day, &year) ;
    	printf ("Item:		Unit		Purchase\n" );
    	printf ("			Price		Date\n" ) ;			
    	Printf ("%d", "%f", "%02d", item, price, month, day, year) ;	
    	
    			
    	return 0 ;
    
    /*program output below*/
    
    Enter item number: 99
    Enter unit price: 99.99
    Enter purchase date (mm/dd/yy): 11/09/91
    Item		Unit		Purchase
    			Price		Date
    
    	
    }
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    This line is wrong:

    Code:
    Printf ("%d", "%f", "%02d", item, price, month, day, year) ;
    
    The format specifier must be all in the same string literal (between the same set of double quotes). Also the number of items your trying to print doesn't match the number of format specifications. (And you have a capital P).

    If you're trying to print in columns, you'll need to include widths in all your specifications and include spaces between them. Perhaps something like this:

    Code:
    printf ("%2d %4.2f %02d/%02d/%02d", item, price, month, day, year) ;
    
    You'll need to play with width and precisions to get the layout you want.

    (As an aside: It's a cardinal sin to accept and use only 2 digit years. Think Y2K bug. Get into the habit of think only of 4 digit years from the beginning).
     
  3. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    Thanks for pointing those mistakes out, I really love that I didn't catch the P instead of p. Anyway, I put the revised code below, but it's not alignment of the %d's I'm worried about right now. It's the word price under unit, and date under purchase. They are both printf strings, and in my code they are aligned, but in the output of the program they are not. See code below, and output below the code. I also experimented in moving the " to in front of the word, that aligned the bottom row of text to the left, still not what I want. I also posted that code and output further down.

    This is how I want it to look........

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

    This is how it came out......

    Item: Unit Purchase
    Price Date


    Experimental modified code below: I moved the first set of quotes...

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

    Output.....

    Item: Unit Purchase
    Price Date
    88, 8.88, 11/01/98

    I want price to be under unit, and date to be under purchase.
     
  4. balamw, Jan 3, 2011
    Last edited: Jan 3, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    It would help if you used CODE tags for your code and output. (The hash/pound mark in the edit window). This makes MR represent things using a monospaced font instead of a variable spaced one.

    EDIT: Is this what you're after?

    Code:
    
    #include <stdio.h>
    
    int main()
    {
    printf ("Item:   Unit    Purchase\n" ); 
    printf ("        Price   Date\n" ) ;	
    }
    $ ./deleteme 
    Item:   Unit    Purchase
            Price   Date
    
    

    B
     
  5. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #5
    Best not to use tabs if you want it to line up. Different editors will expand the tabs to a different number of spaces. macrumors seems to use 7, the editor you're using probably has it set to 4 or 3. In Xcode you can change this in Xcode Preferences / Indenting, or de-select "Tab key inserts tabs, not spaces" and you might get more consistent output.

    Short answer: if alignment matters, use spaces instead of tabs.
     
  6. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #6
    Yes, that's what Im after. I see my code that I copy and pasted didn't stay formatted as I pasted it. When I pasted it there, it was lined up as I mentioned, but seems that once I posted the message to MR, it changed!

    Thanks
    Scott



     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    This is why you need to use the CODE tags when posting code or output from code, and as McGordon suggests, use spaces where possible, not tabs.

    B
     

Share This Page