# Meet some problem in C++ programming

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-14-2011
Ngoo Jia Xun
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;

// 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("+===========================================================+\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;
}

• 06-14-2011
MK27
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++.
• 06-14-2011
Ngoo Jia Xun
Any example?
I'm sorry.. My lecturer keep on say this is C++ programming..
• 06-14-2011
MK27
Quote:

Originally Posted by Ngoo Jia Xun
Any example?
My lecturer keep on say this is C++ programming..

No big deal, but that is 100% C, not C++.
• 06-14-2011
Ngoo Jia Xun
Okay.. Thanks..
So i should change all the floats to int?
• 06-14-2011
CornedBee
Moved to C.
• 06-14-2011
rags_to_riches
• 06-14-2011
MK27
Quote:

Originally Posted by Ngoo Jia Xun
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;
}

• 06-14-2011
CornedBee
Quote:

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.
• 06-14-2011
CommonTater
Quote:

Originally Posted by Ngoo Jia Xun
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.
• 06-14-2011
mike65535
Quote:

Originally Posted by CornedBee
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
• 06-14-2011
Salem
Must be in the same class.
http://cboard.cprogramming.com/cplus...stio-here.html

MK27's answer seems spot on for fixing the rounding problems.
• 06-14-2011
guitargod
Quote:

Originally Posted by Ngoo Jia Xun
Any example?
I'm sorry.. My lecturer keep on say this is C++ programming..