Like Tree2Likes

Meet some problem in C++ programming

This is a discussion on Meet some problem in C++ programming within the C Programming forums, part of the General Programming Boards category; I can't align the table and don't know how to round up the adjusted amount payable.. I tried everything on ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    5

    Meet some problem in C++ programming

    I can't align the table and don't know how to round up the adjusted amount payable.. I tried everything on earth.. Any expert can teach me the solution? TT
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    
    {
    	// Variable declaration
    	int combosetCount, discountVoucher;
    	int a = 37;
    	float amount, amountPayable, cash, changeDue, governmentTax, serviceTax, subTotal, adjustedAmount, priceCombo;
    
    	// Prompt and read the value that entered by user
    	printf("Enter the number of combo set : ");
    	scanf("%d\a", &combosetCount);
    
    	printf("Enter the price per combo set : RM");
    	scanf("%f\a", &priceCombo);
    
    	printf("Enter the discount voucher : RM");
        scanf("%d\a", &discountVoucher);
    
    	printf("Enter Cash : RM");
    	scanf("%f\a", &cash);
     
    	// Calculation
    	amount = priceCombo * combosetCount;
    	governmentTax = amount * 6 / 100;
    	serviceTax = amount * 10/100;
    	subTotal = amount + governmentTax + serviceTax;
    	amountPayable = subTotal - discountVoucher;
    	adjustedAmount = ceilf(amountPayable * 100) / 100;
    	changeDue = cash - adjustedAmount;
    
    	// Combine the results and display the results
    	printf("                          ==========\n");
        printf("                           | KLFC |\n");
    	printf("                          ==========\n");
        printf("             Good Day and Welcome to KLFC Sdn. Bhd\n\n");
    	printf("                        R E C E I P T                        \n");
    	printf("+-------------------------------+----------+----------------+\n");
    	printf("|          Description          |   Rate   |   Amount(RM)   |\n");
    	printf("+-------------------------------+----------+----------------+\n");
    	printf("|        COMBO SET * %3d        |   %3.2f   |     %4.2f      |\n", combosetCount, priceCombo, amount);
    	printf("|        Government Tax         |     6%c   |     %4.2f      |\n", a, governmentTax);
    	printf("|          Service Tax          |    10%c   |     %4.2f      |\n", a, serviceTax);
    	printf("+-------------------------------+----------+----------------+\n");
        printf("|              S U B  T O T A L            |    %-4.2f      |\n", subTotal);
    	printf("+==========================================+================+\n");
    	printf("|   Discount Voucher  (RM)    %5.2d                         |\n", discountVoucher);
    	printf("|                            -------                        |\n");
    	printf("|   Amount Payable            %4.2f                         |\n", amountPayable);
    	printf("|                            =======                        |\n");
    	printf("|   Adjusted Amount Payable                     %-4.2f      |\n",adjustedAmount);
    	printf("+===========================================================+\n");
        printf("              CASH                   %3.2f                   \n", cash);
    	printf("              CHANGE DUE             %3.2f                   \n", changeDue);
    	printf("=============================================================\n\n");
    	printf("                THANK YOU and HAVE A NICE DAY                \n");
    
    
    	return 0;
    }
    Last edited by Ngoo Jia Xun; 06-14-2011 at 06:20 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Do not use floats with money. Money is not a floating point value, it is a fixed precision decimal value. Use integer cents and format that. This will by default round down; to round nearest you need a function to give you a %modulus remainder.

    Also this is C, not C++.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    Any example?
    I'm sorry.. My lecturer keep on say this is C++ programming..

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ngoo Jia Xun View Post
    Any example?
    My lecturer keep on say this is C++ programming..
    No big deal, but that is 100% C, not C++.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    Okay.. Thanks..
    So i should change all the floats to int?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Moved to C.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ngoo Jia Xun View Post
    Okay.. Thanks..
    So i should change all the floats to int?
    Yes. The problem is that 0.1 cannot be exactly represented in floating point, only approximated. Run this:

    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i = 0.0f; i<20; i += 0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    And pay attention to the output. This was a notorious source of bugs in accounting software, historically (or so I've read).

    Anyway, it makes floats a bad choice for fixed precision decimal numbers. Using integer cents, $1.35 = 135. To format that:

    Code:
    #include <stdio.h>
    
    int main(void) {
    	int cents = 135;
    
    	printf("$%d.%d\n",cents/100,cents%100);
    
    	return 0;
    }
    Salem likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And pay attention to the output. This was a notorious source of bugs in accounting software, historically (or so I've read).
    I've also heard that it caused the malfunction of a Patriot missile in the first Gulf war, resulting in the death of 28 US soldiers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ngoo Jia Xun View Post
    Any example?
    I'm sorry.. My lecturer keep on say this is C++ programming..
    It may compile with a C++ compiler... but that don't make it C++ programming.

    I'm thinking you should either A) Get into a real C++ course or B) Get a real professor to teach the class you're in.
    Salem likes this.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by CornedBee View Post
    I've also heard that it caused the malfunction of a Patriot missile in the first Gulf war, resulting in the death of 28 US soldiers.
    The Patriot Missile Failure

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Must be in the same class.
    I Have A Questio here

    MK27's answer seems spot on for fixing the rounding problems.
    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.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Quote Originally Posted by Ngoo Jia Xun View Post
    Any example?
    I'm sorry.. My lecturer keep on say this is C++ programming..
    Kick your lecturer's sorry ass.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    5
    Use ceil and float to round up?

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Quote Originally Posted by mike65535 View Post
    Wow. Instead of multiplying by the inaccurate 0.1 they should have divided by 10. This does not sound like a cumulative rounding error but instead the programmer elected to use floating point constant when there was no use of floating point warranted. An integer divide-by-10 would do the job. And I bet those Patriot Missile programmers get paid more than I do.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem - 2 elevators, when do they meet ?
    By apacz in forum Contests Board
    Replies: 1
    Last Post: 12-03-2005, 11:48 AM
  2. old HD, meet new computer
    By deathstryke in forum Tech Board
    Replies: 12
    Last Post: 12-05-2002, 06:04 PM
  3. if you could meet someone from here in live, who would it be?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 01-19-2002, 07:48 PM

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