trouble with printf alignment

This is a discussion on trouble with printf alignment within the C Programming forums, part of the General Programming Boards category; hi ! can someone help me with the printf alignment when i run the code below , compare with price ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    Unhappy trouble with printf alignment

    hi !
    can someone help me with the printf alignment
    when i run the code below , compare with price = 5 and price=50

    the alignment is like

    $5.00
    $50.00

    how do i change my code , so the alignment is like

    $5.00
    $50.00


    code
    __________________________________________________ __
    #include<stdio.h>

    int main(void){
    float price = 5;
    int quantity =3;
    static char *documentfmt = "%-40s %8s %10s %10s\n";
    printf(documentfmt, "Product Name", "Quantity", "Unit Cost", "Subtotal");
    printf("-----------------------------------------------------------------------\n");
    printf("%49d",quantity);
    printf("%6s%.2f\n","$",price);
    return 0;
    }
    __________________________________________________ __

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    They're kind of identical... if you need to use spaces use a tag like CODE (with brackets around it) to make the text typewriter style, or find a substitute. Spaces disappear automatically in vbb.

    Anyway, you can use printf() format specifiers to buffer a number with spaces. For example:

    printf("%8.2f", 111);

    will display:

    __111.00

    where the _s represent spaces. The 8 means 8 total characters in the output string, buffering with spaces if necessary; the 2 means 2 digits after the decimal. Of course, that means that any dollar sign will be separated from the rest of the number by spaces, but there's no way around that.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    oops

    Oooops



    $50.00
    $5.00

    rather dollar sign assigned together (left alignment)

    i want to have the last digit of the price alligned

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    emmm..........

    i want

    ___$5.00
    __$50.00

    (___ is space)

    rather than

    $5.00
    $50.00

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You can't do that in one single call - but this seems OK
    Code:
    #include <stdio.h>
    
    void foo ( double val ) {
        char buff[20];
        sprintf( buff, "$%.2f", val );  // format to 2dp and prefix with $
        printf( "%8s\n", buff );  // now pad to correct indentation
    }
    int main ( ) {
        foo( 50 );
        foo( 5 );
        return 0;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    thanks salem

    u r the man !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM
  4. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21