Thread: Line up numbers

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    5

    Line up numbers


    How do I line up the numbers a user inputs into a column in such a way that the place values are guaranteed to align, regardless of the size of the integer?

    I get:
    5
    145
    7
    45

    I want:
    _ _5
    145
    __7

    etc
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    Try say in C
    printf("%5d\n", number);

    Or in C++
    cout << setw(5) << number << endl;
    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. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    5
    Originally Posted by salem
    Try say in C
    printf("%5d\n", number);

    Or in C++
    cout << setw(5) << number << endl;
    Nope, doesn't work.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    > Nope, doesn't work.
    Oh what a pity
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    5
    Originally Posted by salem
    > Nope, doesn't work.
    Oh what a pity
    It doesn't change the spacing depending on the number of places. Whenever the numbers print, I get:

    1
    100
    45

    I need:
    __1
    100
    _45

    with __representing spaces. The %5d changes the spacing for all of the numbers, so they're still off balance. The only way I can conceive of doing it is to use a ton of if-statements and printf statements.

    Remember, all values are integers.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    Perhaps you could remember to post your code.

    "It doesn't work" is not a useful description of your problem.
    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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    5
    Originally Posted by salem
    Perhaps you could remember to post your code.

    "It doesn't work" is not a useful description of your problem.
    Here you go!
    Code:
    for(i=0;i<size;i++)
    {
    	printf("     %5d     %5d\n",x[i],s[i]);
    }
    			printf("\nValue  Count\n");
    			
    for(i=0;i<d+1;i++)
    {
    	printf("   %5d -        %5d\n",v[i],c[i]);
    }
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    No idea - it works for me.
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        int x[10] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
        int i;
        for ( i = 0 ; i < 10 ; i++ ) {
            printf("%5d\n", x[i] );
        }
        return 0;
    }
    $ gcc foo.c
    $ ./a.out 
        1
        2
        5
       10
       20
       50
      100
      200
      500
     1000
    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
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    5
    Originally Posted by salem
    No idea - it works for me.
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        int x[10] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
        int i;
        for ( i = 0 ; i < 10 ; i++ ) {
            printf("%5d\n", x[i] );
        }
        return 0;
    }
    $ gcc foo.c
    $ ./a.out 
        1
        2
        5
       10
       20
       50
      100
      200
      500
     1000
    Could the problem be that I'm trying to print out two columns?
    I'm trying to get both columns, minus the "__" to be:


    1 __1
    5 __5
    10 __14
    20 __18
    50 __145
    100 etc
    200
    500
    1000
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    One way to handle this is to check the return value of printf(). It returns the number of characters that it printed. That way, you can figure out how many spaces to print before printing the second number.
    Code:
    #define INDENT_WIDTH 10
    int i, idx;
    
    idx = printf("%d", first_number);
    /* Now print as many spaces as needed to get to the next column */
    for (i = idx; i < INDENT_WIDTH; i++) {
        printf(" ");
    }
    /* Now print the second number */
    printf("%d\n", second_number);
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by Astrodude
    Could the problem be that I'm trying to print out two columns?
    I'm confused. Multiple columns should line up as long as you don't exceed the field width. I saw your earlier post where you posted some code, but I still don't see what the problem is.
    Code:
    % cat test.c
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        int x[10] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
        int i;
        for ( i = 0 ; i < 10 ; i++ ) {
            printf("%5d  %5d  %5d\n", x[i], x[i]*3, x[i]*7);
        }
        return 0;
    }
    
    % gcc -ansi -Wall -pedantic test.c
    % ./a.out
        1      3      7
        2      6     14
        5     15     35
       10     30     70
       20     60    140
       50    150    350
      100    300    700
      200    600   1400
      500   1500   3500
     1000   3000   7000
  22. #12
  23. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    > Could the problem be that I'm trying to print out two columns?
    No, the problem is you can't be bothered to add a few more lines to your test code to make it a COMPLETE program we can run, and see the problem that you're seeing.

    So instead there have been 3 separate guesses at trying to figure out what possible reason "%5d" isn't lining up for you.
    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

IMN logo majestic logo threadwatch logo seochat tools logo