How to right justify a column

Discussion in 'C Programming' started by jwatts@berkeley.edu, Jul 23, 2003.

  1. Guest

    Hello group,

    I am feeling pretty good about this program. I am just learning and
    am trying to get the basics down. I would like to ask if someone
    could tell me how to right justify the BALANCE column in my program's
    output.

    ===============
    Here is my code:
    ===============

    #include <stdio.h>

    int main (void)
    {


    int i;
    int accountNumber[10];
    char lastName[10][30];
    float accountBalance[10];

    printf("Enter account number, last name, and balance.\n");
    printf("Enter -999 to end input.\n");


    for (i = 0; i < 10; i++)
    {

    printf("? ");
    scanf ("%i", &accountNumber);

    if (accountNumber == -999)
    {
    printf("\n");

    break;
    }

    scanf("%s", lastName);
    scanf("%f", &accountBalance);

    } /* End for loop */

    printf("%-15s%-15s%-15s\n","ACCOUNT","LAST NAME","BALANCE");

    for(i = 0; (i < 10) && (accountNumber != -999); i++)
    {

    printf("%-15d%-15s%-15.2f", accountNumber,
    lastName, accountBalance);

    printf("\n");
    }

    printf("\n\n");

    return 0;

    } /* End main */
     
    , Jul 23, 2003
    #1
    1. Advertising

  2. Shanmu Guest

    wrote:
    > Hello group,
    >
    > I am feeling pretty good about this program. I am just learning and
    > am trying to get the basics down. I would like to ask if someone
    > could tell me how to right justify the BALANCE column in my program's
    > output.
    >
    > ===============
    > Here is my code:
    > ===============
    >
    > #include <stdio.h>
    >
    > int main (void)
    > {
    >
    >
    > int i;
    > int accountNumber[10];
    > char lastName[10][30];
    > float accountBalance[10];
    >
    > printf("Enter account number, last name, and balance.\n");
    > printf("Enter -999 to end input.\n");
    >
    >
    > for (i = 0; i < 10; i++)
    > {
    >
    > printf("? ");
    > scanf ("%i", &accountNumber);
    >
    > if (accountNumber == -999)
    > {
    > printf("\n");
    >
    > break;
    > }
    >
    > scanf("%s", lastName);
    > scanf("%f", &accountBalance);
    >
    > } /* End for loop */
    >
    > printf("%-15s%-15s%-15s\n","ACCOUNT","LAST NAME","BALANCE");

    Use: printf("%-15s%-15s%15s\n","ACCOUNT","LAST NAME","BALANCE");
    >
    > for(i = 0; (i < 10) && (accountNumber != -999); i++)
    > {
    >
    > printf("%-15d%-15s%-15.2f", accountNumber,
    > lastName, accountBalance);

    Use: printf("%-15d%-15s%15.2f", accountNumber,
    lastName, accountBalance);
    >
    > printf("\n");
    > }
    >
    > printf("\n\n");
    >
    > return 0;
    >
    > } /* End main */

    Using the - modifier in the format left justifies the result within
    the field. Removing it causes right justification.

    Rgds,
    Shanmu.
     
    Shanmu, Jul 23, 2003
    #2
    1. Advertising

  3. Guest

    Shanmu,

    Wow do I feel silly. I kept looking around the Internet for how to do
    it. No wonder I couldn't find it considering it happens by default.
    Oh well, thanks for helping me out. :eek:)
     
    , Jul 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tina

    Right Justify Hyperlink Buttons?

    Tina, Dec 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    617
    G Dean Blake
    Dec 9, 2004
  2. John Wilson

    right justify a column?

    John Wilson, Jun 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    675
    John Wilson
    Jun 22, 2005
  3. John Wilson

    Right justify text in textbox in web page

    John Wilson, Oct 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    6,561
    John Wilson
    Oct 8, 2005
  4. Norman
    Replies:
    15
    Views:
    4,307
    Andrew
    Mar 6, 2007
  5. Replies:
    1
    Views:
    1,143
    Beauregard T. Shagnasty
    May 1, 2007
Loading...

Share This Page