For some reason, printf function adds a space to what I want to display

JC8

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.

Iharob Al Asimi

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

For some reason, printf function adds a space to what I want to display

From Dev

For what reason printf does not correctly display the float?

From Dev

For what reason printf does not correctly display the float?

From Dev

what will translate function do if I want to change some chars to nothing?

From Dev

For some reason my code adds 2 to a variable instead of 1 and I don't know why

From Dev

For some reason my code adds 2 to a variable instead of 1 and I don't know why

From Dev

I need to use this function in a program but the output is freezing for some reason

From Dev

Recursive function not returning what I want

From Dev

Recursive function not returning what I want

From Dev

Python: Help, function not producing what I want

From Dev

i want to be able to set workbook from a cell value, it doesn't seem to work for some reason

From Dev

i want to be able to set workbook from a cell value, it doesn't seem to work for some reason

From Dev

Why SVG adds some space inside it?

From Dev

printf("%d") doesn't display what I input

From Dev

CSS some space at the end of the page, reason floats?

From Dev

I want to understand the exact reason why overflow:hidden and display:inline-block is used here

From Dev

I want to understand the exact reason why overflow:hidden and display:inline-block is used here

From Dev

What are some of the issues with CCSM and why would I want to avoid it?

From Dev

I want that whenever I press SPACE, a new pygame window will pop up. But the program is showing some errors whenever i press SPACE

From Dev

Document.write won't display what I want

From Dev

What if the owner of some repository on github has stopped working on some project but I want to contribute to it?

From Dev

Android with Eclipse - If condition fails for some unknown reason. What am I missing?

From Dev

After upgrading to 19.04, what should i do with all PPA's that has been disabled for some reason?

From Dev

What's the reason for tying some applications to GNOME?

From Dev

What's the reason for tying some applications to GNOME?

From Dev

Can't make split() function to do what I want

From Dev

R function doesn't do what I want

From Dev

Java, what if I want to return different types from function?

From Dev

javascript does not for some reason want to execute on my machine

Related Related

  1. 1

    For some reason, printf function adds a space to what I want to display

  2. 2

    For what reason printf does not correctly display the float?

  3. 3

    For what reason printf does not correctly display the float?

  4. 4

    what will translate function do if I want to change some chars to nothing?

  5. 5

    For some reason my code adds 2 to a variable instead of 1 and I don't know why

  6. 6

    For some reason my code adds 2 to a variable instead of 1 and I don't know why

  7. 7

    I need to use this function in a program but the output is freezing for some reason

  8. 8

    Recursive function not returning what I want

  9. 9

    Recursive function not returning what I want

  10. 10

    Python: Help, function not producing what I want

  11. 11

    i want to be able to set workbook from a cell value, it doesn't seem to work for some reason

  12. 12

    i want to be able to set workbook from a cell value, it doesn't seem to work for some reason

  13. 13

    Why SVG adds some space inside it?

  14. 14

    printf("%d") doesn't display what I input

  15. 15

    CSS some space at the end of the page, reason floats?

  16. 16

    I want to understand the exact reason why overflow:hidden and display:inline-block is used here

  17. 17

    I want to understand the exact reason why overflow:hidden and display:inline-block is used here

  18. 18

    What are some of the issues with CCSM and why would I want to avoid it?

  19. 19

    I want that whenever I press SPACE, a new pygame window will pop up. But the program is showing some errors whenever i press SPACE

  20. 20

    Document.write won't display what I want

  21. 21

    What if the owner of some repository on github has stopped working on some project but I want to contribute to it?

  22. 22

    Android with Eclipse - If condition fails for some unknown reason. What am I missing?

  23. 23

    After upgrading to 19.04, what should i do with all PPA's that has been disabled for some reason?

  24. 24

    What's the reason for tying some applications to GNOME?

  25. 25

    What's the reason for tying some applications to GNOME?

  26. 26

    Can't make split() function to do what I want

  27. 27

    R function doesn't do what I want

  28. 28

    Java, what if I want to return different types from function?

  29. 29

    javascript does not for some reason want to execute on my machine

HotTag

Archive