Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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

	
}
 
Last edited by a moderator:

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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).
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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
 
Last edited:

McGordon

macrumors member
Dec 28, 2010
63
1
Scotland
I used tab stops in writing my code, which looks perfect on the code, but not when the program is run.

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.
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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



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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.