Thread: formatting output

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    formatting output

    ugh, more problems here.

    now i am trying to format the output and am having trouble getting things to look the way i would like them too.

    here is my code:

    Code:
    #include <stdio.h>
    
    /* Define structure */
    struct info
    {
    	long int accntNum;
    	char name[25];
    	float balance;
    };
    
    /* Begin main */
    main()
    {
    	/* Declare variables */
    	struct info client[25];
    	int x, numClient;
    	
    	/* Display opening message */
    	printf ("Welcome to Client Account Information Services\n\n");
    	
    	/* Prompt for how many clients will have info entered */
    	printf ("Enter number of clients to be used: ");
    	scanf ("%i", &numClient);
    	
    	/* Prompt for user to enter each clients info */
    	for (x = 0; x < numClient; x++)
    	{
    		printf ("\nEnter account number: ", x + 1);
    		scanf ("%ld", &client[x].accntNum);
    		
    		fflush(stdin); /* Remove extraneous characters */
    		printf ("Enter last name: ", x + 1);
    		gets (client[x].name);
    		fflush(stdin); /* Remove extraneous characters */
    		
    		printf ("Enter balance: ", x + 1);
    		scanf ("%f", &client[x].balance);
    	} /* end for */
    	
    	/* Display each clients information */
    	printf ("\nACCOUNT    LAST NAME    BALANCE");
    	for (x = 0; x < numClient; x++)
    	{
    		printf ("\n%-4ld       %s     %12.2f", client[x].accntNum, client[x].name, client[x].balance);
    	} /* end for */
    	
    	printf ("\n");
    	return 0;
    	
    } /* end main */
    my output is not being aligned like i would like it. its coming out like this:

    Code:
    Welcome to Client Account Information Services
    
    Enter number of clients to be used: 2
    
    Enter account number: 100
    Enter last name: washington
    Enter balance: 100
    
    Enter account number: 200
    Enter last name: smith
    Enter balance: 200
    
    ACCOUNT    LAST NAME        BALANCE
    100        washington           100.00      <----how can i get the balance
    200        smith           200.00                       number to line up the decimals?
    Press any key to continue . . .
    basically i need to right justify the balance number so the decimal points line up.

    i have not been able to get this to work so far. i need the last names to line up by the first letter so using something like %15s is out of the question because that lines up the names by the last letter.

    any help is appreciated!

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    in case you cant understand, this is how i want the output to look:

    Code:
    ACCOUNT    LAST NAME        BALANCE
    100        washington       100.00      
    200        smith            200.00
    i can either get the decimal points to line up but the first letter of the names wont, or the first letter of the names to line up and not the decimal points.
    Last edited by kisiellll; 04-04-2009 at 03:35 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kisiellll View Post
    in case you cant understand, this is how i want the output to look:

    Code:
    ACCOUNT    LAST NAME        BALANCE
    100        washington       100.00      
    200        smith            200.00
    i can either get the decimal points to line up but the first letter of the names wont, or the first letter of the names to line up and not the decimal points.
    Print with fixed width fields:
    "ACCOUNT" has 7 letters and looks like you want 3 spaces before the start of "Last Name"
    Code:
    printf("%10d %14s %9.2f", account, name, balance);
    If you have a name longer than 14 char's, just increase the field width. Remember that minus and plus char's change the justification of the variable, within it's field.


    fflush(stdin) is not supported anymore, so it probably won't work well. You can't flush() an input stream reliably, but you can "pull" any left over char's, from it:

    getchar() right after the scanf() of a char or a number, is one way to do it, since it takes the newline (enter key), char still on the
    keyboard buffer, and pulls it off.

    You have complete control with printf(). I've even done "movie credit" style formatting on lists with it.
    Last edited by Adak; 04-04-2009 at 04:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM