Thread: Question about table formatting in C.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    44

    Question about table formatting in C.

    I am writing a program that lets the user enter the name and surname of his employees. I also need to put this information in a table , under the headings NAME SURNAME, where the names would be put under the NAME headline and surnames and the SURNAME headlines.

    The problem that I am finding is that when entering long names, the surname is also moving to the right, and hence it will no longer be under the SURNAME headline. My question is : How can I reserve 10 spaces for the name, so that the SURNAME won't move to the right ?

    This is what is happening :

    NAME SURNAME
    Alan___________Smith <-- I want the surname to be exactly under the heading.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    A convenient way to solve it is to use the formatting options of printf to specify a column width
    Example:
    Code:
    printf("%-20s %-20s\n", "First name", "Last name");
    printf("%-20s %-20s\n", "Bob", "Smith");
    printf("%-20s %-20s\n", "Mary", "Smith");
    If you need to change the widths of your fields, change the number 20.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read the manual page for printf.

    In particular, understand what these variations do
    Code:
    printf("%s\n", name );
    printf("%10s\n", name );
    printf("%.10s\n", name );
    printf("%10.10s\n", name );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    Thanks a lot Salem. Just what I needed !

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    Now I have floating point representations, and I have to restrict the numbers to 1 d.p . So I did %.1f . How can I use the above technique with such representations please?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    That is what I do not like when solutions are given away like this. Next time the guy who asked encounters a problem, he won't search for the solution, but he is going to ask again.

    Teach someone how to search for answers himself.

    Texaskid here is the ref of printf. Read it. Try different formations to see which one fits your needs.

    Also, notice that my point is not this: "Don't ask again, search by yourself!"

    My point is: "Search by yourself and if you don't succeed, ask!"

    //When following this advice, it would be nice of you, if you would post your attempt and you have already tried, so that people do not try the same
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick formatting question
    By Xeric in forum C Programming
    Replies: 4
    Last Post: 03-16-2012, 03:29 PM
  2. Simple formatting question..
    By Mr Moe in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2005, 09:25 AM
  3. table formatting
    By kurz7 in forum C Programming
    Replies: 1
    Last Post: 01-14-2003, 06:12 AM
  4. Question about formatting flags
    By dpro in forum C Programming
    Replies: 2
    Last Post: 12-24-2002, 04:46 PM
  5. formatting question..
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 01:01 PM