Reputation: 43
I'm trying to display text in columns, but on the second line of the output a space gets added. Am I doing anything wrong? (The entries do not contain a space before the string) Here are the functions involved:
add(List *book)
{
Contact *runner = book->una;
Contact *node = (Contact *) malloc(sizeof(Contact));
if (node == NULL);
else {
printf("Add a contact\n");
printf("Enter the first name: ");
scanf("%s", node->fname);
printf("Enter the last name: ");
scanf("%s", node->lname);
printf("Enter the mobile number: ");
scanf("%s", node->number);
printf("\nContact added!\n\n");
node -> next = NULL;
if(book->una == NULL){
book->una = node;
}
else
{
while( runner->next != NULL)
{
runner = runner->next;
}
runner->next = node;
}
}
}
display(List *book)
{
Contact *runner = book -> una;
if(runner == NULL)
{
printf("%20s", "PHONEBOOK EMPTY!");
return;
}
printf("Contact list\n");
printf("%-15s%-15s%-15s", "First Name", "Last Name", "Mobile number\n");
while( runner != NULL)
{
printf("%-15s%-15s%-15s\n", runner->fname, runner->lname, runner->number);
runner = runner->next;
}
}
/*An example output basically turns out like this:
First Name Last Name Mobile Number
James Harrison 123456
Wendy Barnes 00000
Cam Rodriguez 575938*/
Any input data will be printed as shown above.
Upvotes: 2
Views: 67
Reputation: 53036
You should put the \n
in the format string instead of the field. Since Mobile number
has 13 characters, plus the \n
14 and one space is added as padding for the %-15s
, it goes after the \n
hence, in the next line.
Upvotes: 9