Thread: Frustated with mess table printing

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Talking Frustated with mess table printing

    hi, I'm newbie here, this is my first thread, and my first post,

    NICE TO MEET YOU GUYS!

    Firstly, I want to apologize if my english is not easy to understand (I'm Indonesian who study in US).
    ok, I have a problem with printing the problem, i have search many thread but I got no any answer.

    look at this code,
    Code:
    void PrintOut (char First[], char Last[], int Birth, int Swimming, int Tennis, 
                   int Horse, int Craft, int Total )
    {
       printf("%0s          %0s           %6d        %2d      %2d     %2d     %2d           %2d\n",
               First,       Last,       Birth,  Swimming,  Tennis,  Horse,  Craft,  Total);
    }
    if I input the data like this,
    Code:
    Enter the number of people attend: 3
    
    Enter the first name: benedict
    Enter the last name: tobias
    Enter the birth date: 190990
    Enter the swimming skill score: 10
    Enter the tennis skill score: 9
    Enter the horsemanship skill score: 9
    Enter the crafts skill score: 0
    
    Enter the first name: ben
    Enter the last name: tob
    Enter the birth date: 209900
    Enter the swimming skill score: 1
    Enter the tennis skill score: 1
    Enter the horsemanship skill score: 1
    Enter the crafts skill score: 1
    
    Enter the first name: tobias
    Enter the last name: ben
    Enter the birth date: 102939
    Enter the swimming skill score: 10
    Enter the tennis skill score: 10
    Enter the horsemanship skill score: 10
    Enter the crafts skill score: 10
    the result after I input the data is printed like this
    Code:
    First Name   Last Name   Birth   Swimming   Tennis   Horse   Craft   Total Score
    benedict          tobias           190990        10       9      9       0           28
    ben          tob           209900         1       1      1      1              4
    tobias          ben           102939        10      10     10     10           40
    instead of,
    Code:
    First Name   Last Name   Birth   Swimming   Tennis   Horse   Craft   Total Score
    benedict     tobias      190990        10        9       9       0            28
    ben          tob         209900        1         1       1       1             4
    tobias       ben         102939        10       10      10      10            40
    my question is, how to print a neat table like i want? I have trouble about space, if I put only 3 character in "Last Name", all of the entire score will move..
    This is the entire code:
    Code:
    #include <stdio.h>
    
    int  PeopleAttend ();
    int  Compute      (int a, int b, int c, int d);  
    void GetInput     (char *First[], char *Last[], int *Birth);
    void GetScore     (int *Swimming, int *Tennis, int *Horse, int *Craft);
    void PrintOut     (char First[], char Last[], int Birth, int Swimming, int Tennis, 
                       int Horse, int Craft, int Total );
    void Heading      ();
    void Ending       ();
    
    int main()
    {
       char FirstName [16][20];
       char LastName  [16][20];
       int  BirthDate [20];
       int  Swimming  [20];
       int  Tennis    [20];
       int  Horse     [20];
       int  Craft     [20];
       int  People;
       int  TotalScore[20];
       int  PeopleCounter;
    
       PeopleCounter = PeopleAttend();
       
       for (People = 0; People < PeopleCounter; People++)
          { 
             GetInput(&FirstName[People], &LastName[People], &BirthDate[People]);
    
             GetScore(&Swimming[People], &Tennis[People], &Horse[People], &Craft[People]); 
    
             TotalScore[People] = Compute(Swimming[People], Tennis[People], Horse[People], 
                                          Craft[People]);
          }
    
       Heading();
       for (People = 0; People < PeopleCounter; People++)
          {     
             PrintOut (FirstName[People], LastName[People], BirthDate[People], Swimming[People], 
                       Tennis[People], Horse[People], Craft[People], TotalScore[People]);
          }
       Ending();
    
    }
    
    int PeopleAttend()
    {
       int x;
    
       printf("Enter the number of people attend: ");
       scanf (" %d", &x);
    
       return x;
    } 
    
    int  Compute (int a, int b, int c, int d) 
    {
       int x;
       
       x = a + b + c + d;
       
       return x;
    }
    
    void GetInput (char *First[], char *Last[], int *Birth)
    {
       printf("Enter the first name: ");
       scanf (" %s", First);
    
       printf("Enter the last name: ");
       scanf (" %s", Last);
    
       printf("Enter the birth date: ");
       scanf (" %d", Birth);
     
    }
    
    void GetScore (int *Swimming, int *Tennis, int *Horse, int *Craft)
    {  
       printf("Enter the swimming skill score: ");
       scanf (" %d", Swimming);
    
       printf("Enter the tennis skill score: ");
       scanf (" %d", Tennis);
    
       printf("Enter the horsemanship skill score: ");
       scanf (" %d", Horse);
    
       printf("Enter the crafts skill score: ");
       scanf (" %d", Craft);
    }
    
    void Heading ()
    {
       printf("\t\t\t\tSUMMER REPORT CAMPERR\n\n");
       printf("First Name   Last Name   Birth   Swimming   Tennis   Horse   Craft   Total Score\n");
       
    }
    
    void PrintOut (char First[], char Last[], int Birth, int Swimming, int Tennis, 
                   int Horse, int Craft, int Total )
    {
       printf("%0s          %0s           %6d        %2d      %2d     %2d     %2d           %2d\n",
               First,       Last,       Birth,  Swimming,  Tennis,  Horse,  Craft,  Total);
    }
    
    void Ending ()
    {
       printf("---------------------------------------------------------------------------");
    }
    Thank You.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    anyone can help me?
    Thanks

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try using tabs ( \t )instead of hard coding in spaces.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Tabs work well if the names are about the same length. If the ends of the name wind up in a different tab zone however, then tabs won't work for you.

    You have complete control by using the printf() width of field specifier, which goes after the % sign. Set that to the longest name you have + 1. Then use the +- before it, to set your left or right justification, within that field. This goes right before the width of field specifier. (printf() has default setting for strings and numbers, but you may want to override those settings).

    Say for instance, that you want other numbers to stay out of the "Swimming" column.

    So set swimming score to print in a field that is 8 char's wide. Even though the swimming numbers may be only 2 digits in width, set the field for printing it, to 8 char's.
    Code:
    Swimming      Tennis
    =====================
    xxxxxx23       xxx34
    The x's show the width of field being used to hold the swimming and tennis scores, in their own columns.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Quote Originally Posted by Adak View Post
    Tabs work well if the names are about the same length. If the ends of the name wind up in a different tab zone however, then tabs won't work for you.

    You have complete control by using the printf() width of field specifier, which goes after the % sign. Set that to the longest name you have + 1. Then use the +- before it, to set your left or right justification, within that field. This goes right before the width of field specifier. (printf() has default setting for strings and numbers, but you may want to override those settings).

    Say for instance, that you want other numbers to stay out of the "Swimming" column.

    So set swimming score to print in a field that is 8 char's wide. Even though the swimming numbers may be only 2 digits in width, set the field for printing it, to 8 char's.
    Code:
    Swimming      Tennis
    =====================
    xxxxxx23       xxx34
    The x's show the width of field being used to hold the swimming and tennis scores, in their own columns.
    so, the code for 8 space print like this?
    Code:
    printf("%8d");
    or
    Code:
    printf("%-8d");
    sorry, i'm C programmer kiddie..

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       char FirstName [16][20];
       char LastName  [16][20];
    Looks bass ackwards to me.
    Everything else ther e is 20 of, but you have 16 strings of 20 characters here...

    Of coruse, all of this:
    Code:
       char FirstName [16][20];
       char LastName  [16][20];
       int  BirthDate [20];
       int  Swimming  [20];
       int  Tennis    [20];
       int  Horse     [20];
       int  Craft     [20];
       int  TotalScore[20];
    should really be a structure, not several parallel arrays.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    It should be

    Code:
    printf("%8d");
    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  2. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  3. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  4. Printing out a data table in different sequences
    By TheDudeAbides in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 12:06 AM
  5. Printing characters not in the ASCII table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 01:47 PM