Thread: aligning a table?

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    aligning a table?

    I'm having trouble aligning a table

    Code:
    /* unaligned table */
    
    City                 | x coord | y coord
    ---------------------+---------+---------
      Nowhere               28.80     17.60
    Somewhere                5.60     79.12
     Whoknows               55.30     49.90
    Elsewhere               22.22     11.11
    
    
    /* aligned table */
    
    City                 | x coord | y coord
    ---------------------+---------+---------
    Elsewhere                22.22     11.11
    Nowhere                  28.80     17.60
    Whoknows                 55.30     49.90
    Somewhere                 5.60     79.12

    The tricky part is, the file is dynamic so if i change the contents the alignment is broken:

    Code:
    /* unaligned table */
    
    City                 | x coord | y coord
    ---------------------+---------+---------
        city1                4.40      7.70
        city2                3.30      8.80
    
    /* aligned table */
    
    City                 | x coord | y coord
    ---------------------+---------+---------
    city1                     4.40      7.70
    city2                     3.30      8.80


    Here's what my code looks like for the unaligned version:

    Code:
        printf("City                 | x coord | y coord");
         printf("\n---------------+---------+---------\n");
          printf("%9s %15.2f %9.2f\n",     startptr->cityname,startptr->xcp, start-ptr>yco);

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Just use %-9s, then? It will left justify it, using a minimum of 9 characters.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks. Hmm this is strange:

    Code:
    City                 | x coord | y coord
    ---------------------+---------+---------
    city1                   4.40       7.70
    city2                   3.30       8.80
    works fine

    this:

    Code:
    City                 | x coord | y coord
    ---------------------+---------+---------
    Nowhere                28.80      17.60
    Somewhere                 5.60      79.12
    Whoknows                55.30      49.90
    Elsewhere                22.22      11.11
    doesn't...

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Works for me. My code:
    Code:
    #include <stdio.h>
    
    struct foo {
            char *bar;
            double v1, v2;
    } baz[] = {
            {"Nowhere", 28.80, 17.60},
            {"Somewhere", 5.6, 79.12},
            {"Whoknows",55.3, 49.9},
            {"Elsewhere",22.22,11.11},
            {NULL,0,0}
    };
    
    int main(void)
    {
            struct foo *quux = baz;
    
            while (quux->bar)
            {
                    printf("%-9s %15.2f %9.2f\n", quux->bar, quux->v1, quux->v2);
                    quux++;
            }
            return 0;
    }
    My output:
    Code:
    Nowhere             28.80     17.60
    Somewhere            5.60     79.12
    Whoknows            55.30     49.90
    Elsewhere           22.22     11.11

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    yup you're right. But

    Code:
    City                 | x coord | y coord
    ---------------------+---------+--------
    Elsewhere               22.22     11.11
    Sidknee                147.00     34.00
    Gotham                  83.70     26.80
    Somewhere                5.60     79.12
    Whoknows                55.30     49.90
    Trantor                  0.00      0.00
    Metropolis               12.56     12.09
    Smallville               16.86      5.20
    Nowhere                 28.80     17.60
    no idea why it's not working. It's basically the same as above with a few more stuff added.

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok changed the min character length to 10 and it works now thanks.

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. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM
  3. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  5. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 03:49 PM