Thread: print out proper aligned tect...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    print out proper aligned tect...

    hi,

    How do i print out the text such that the alignment is always there regardless of how long the name of person is...
    From :
    Code:
    Records     
    ----------
    
    ID     Name         DateOfBirth       Height     Weight
    01     jenny                 21/01/81          1.68        45
    
    
    To :
    Records
    ----------
    
    ID     Name         DateOfBirth       Height     Weight
    01     jenny         21/01/81          1.68        45

    my code is :
    printf("ID \t Name \t DateOfBirth \t Height \t Weight\n");


    printf("%d \t %s \t %d/%d/%d \t %-0.2f \t %-0.1f\n",HobbitsArray[i].hobbitID, HobbitsArray[i].name, HobbitsArray[i].dateOfBirth.day,HobbitsArray[i].dateOfBirth.month,HobbitsArray[i].dateOfBirth.year, HobbitsArray[i].height, HobbitsArray[i].weight);
    i++;
    }


    CODE TAGS added by Hammer

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    15
    if i enter jen as name...its still nice...if i entered jenny lee..it goes hayward...


    ID Name DateOfBirth Height Weight
    01 jen 21/01/81 1.68 45




    ID Name DateOfBirth Height Weight
    01 jenny lee 21/01/81 1.68 45

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You could define a maximum name-length and declare an array of char to store names of that length. Copy the name in the array and if it does not take all memory, then fill the rest with spaces. In this way you always print a fixed length name.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shiro
    You could define a maximum name-length and declare an array of char to store names of that length. Copy the name in the array and if it does not take all memory, then fill the rest with spaces. In this way you always print a fixed length name.
    Or you could just use the printf() format modifiers.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to print even numbers
    By ortegac in forum C Programming
    Replies: 3
    Last Post: 05-21-2006, 12:01 AM
  2. Replies: 14
    Last Post: 01-12-2006, 05:08 AM
  3. Bypassing Print Dialog Box
    By leojose in forum Windows Programming
    Replies: 0
    Last Post: 10-13-2005, 06:44 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM