How to right justify a column

J

jwatts

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 */
 
S

Shanmu

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.
 
J

jwatts

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:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top