Menu

how to align the decimal of the out put?

2010-07-18
2012-09-26
  • jordan pagkatipunan

    Sample of expected output:

    Enter Sales Amount: 274.25
    VAT: 32.91
    LGT: 9.21

    Total Amount Due: 316.37

    this is my program:

    MY CODE:

    include<stdio.h>

    include<conio.h>

    include<math.h>

    main()
    {
    float VAT, LGT, TOTAL, Amount;

    printf("Enter Sales Amount: \t ");

    scanf("%f", &Amount);

    VAT = .12Amount;
    LGT = .03
    Amount;
    TOTAL = Amount+LGT+VAT;

    printf("\nVAT: \t%6.2f", VAT);
    printf("\nLGT: \t%6.2f", LGT);
    printf("\n\nTotal Amount Due:\t%6.2f", TOTAL);
    getch();
    }

    i cant get an perfect alignment on my decimals, my goal is to have an align
    decimals on any of my input and out put.
    thats my teacher saids, im beginner here in C++ and im using dev c++, please
    help me to my project and thx, guys

     
  • jordan pagkatipunan

    Enter Sales Amount: 274.25
    VAT: 32.91
    LGT: 9.21

    Total Amount Due: 316.37

     
  • jordan pagkatipunan

    ooopps this is my sample expected out put:

    Enter Sales Amount: 274.25
    VAT: 32.91
    LGT: 9.21

    Total Amount Due: 316.37

     
  • jordan pagkatipunan

    Enter Sales Amount:----274.25
    VAT:----------------------------32.91
    LGT:-----------------------------9.21

    Total Amount Due:-------316.37

    sorry , THIS SHOULD BE THE EXAMPLE OUTPUT>..

     
  • cpns

    cpns - 2010-07-18

    I would suggest that you sprintf the value to a string using "%.2f". The
    return value of sprintf is the number of characters output (i.e. the length of
    the string). You can then use this value it calculate the number of spaces to
    output before finally outputting the string. I suggest that you create a
    function to do this:

    void printdecimalalign( float value, int decpos ) ;
    

    where decpos _is the number fo characters _relative to the current cursor
    position to place the decimal, and value is the number to print.

    Some other points:

    User code tags to post code.

    Your screenshot uses different user input that your example. Also it is an
    unreasonable value for a single precision float, which is accurate to only 6
    significant figures.

    None of your code requires <math.h>. <conio.h> and getch() are not standard;
    use caution.

    You should explicitly declare the return type of main(), prefereably as int.

    Your code does not compile without warnings. Use teh f suffix on float literal
    constants to force type agreement e.g:

     VAT = .12f * Amount;
    

    or of course use a double. There is little point in using a float; it may have
    been significant on ancient systems with no FPU and slow memory, or where
    computational speed is critical, but not in this case.

     
  • cpns

    cpns - 2010-07-18

    ... using code tags for your "example output" would be a good idea too!

     
  • cpns

    cpns - 2010-07-19

    ... that is to say did you mean this?:

    Enter Sales Amount:     274.25 
    VAT:                     32.91 
    LGT:                      9.21
    
    Total Amount Due:       316.37
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.