I am still new to C programming and I am having a bit of a problem in my coding. I am aware that I can't just ask for help without trying to fix it by myself first, so I tried fixing it and is still having the same problem.
My problem is, my coding displayed an incorrect calculation like below.
ORDER PRICE SUMMARY
===========================================
Customer No. Total Order Price(RM)
===========================================
1 RM 40.00
2 RM 40.00
3 RM 478664371470163710000000000000000.00
===========================================
Grand Total RM 478664371470163710000000000000000.00
===========================================
Customer 1 and 2 showed the correct price but it just got messed up for Customer 3, which affects the grand total.
My coding is below, and it is obvious that I am using the simplest way of writing my code since my goal is to just answer a coding question.
#include <stdio.h>
float calculatePrice(char);
void main()
{
char comboType, addon;
int comboQty, addonQty,i=1,j=1;
float orderPrice = 0.00, comboPrice, addonPrice, grandPrice=0.00; // new float
float customerPayment[3], allPrice[3]; // declare array
printf ("--------------------------------------------------------------\n");
printf (" ~ SATAY RESTAURANT ~ \n");
printf ("--------------------------------------------------------------\n");
printf (" Combo Type Item Price (RM) \n");
printf ("--------------------------------------------------------------\n");
printf (" A 25 Chicken Stay + 25 Beef Satay 40.00\n");
printf (" B 30 Chicken Stay + 20 Mutton Satay 52.00\n");
printf (" C 10 Mutton Stay + 40 Beef Satay 46.00\n");
printf (" Add-On 1 Ketupat 0.60\n");
printf ("--------------------------------------------------------------\n");
for (j=0;j<3;j++)
{
printf("\n Customer %d\n",j+1);
printf("-------------");
printf("\n Enter Combo Type (A/B/C or X to end) : ");
scanf(" %c", &comboType);
while (comboType != 'X' && comboType != 'x') // start while loop
{
comboPrice = calculatePrice (comboType);
printf(" Order Price for Combo %c : RM%.2f \n ", comboType, comboPrice);
printf("\n Enter Combo Type (A/B/C or X to end) : ");
scanf(" %c", &comboType);
allPrice[j] += comboPrice;
}
printf("\n Add-On Ketupat (Y/N) : "); // if X, ask for add-on
scanf(" %c", &addon);
if (addon != 'N' && addon != 'n')
{
printf(" Enter Ketupat Quantity : ");
scanf("%d", &addonQty);
addonPrice = 0.60 * addonQty;
allPrice[j] += addonPrice;
printf(" Order price for Ketupat : RM%.2f\n",addonPrice);
}
printf("\n Total Order Price for Customer %d : RM%.2f\n\n",j+1, allPrice[j]);
customerPayment[j] = allPrice[j];
}
printf("\n\n\n\t ORDER PRICE SUMMARY ");
printf("\n===========================================");
printf("\n Customer No. Total Order Price(RM)");
printf("\n===========================================");
for (i = 0; i<3; i++)
{
printf("\n %d RM %.2f",i+1,customerPayment[i]);
grandPrice += customerPayment[i];
}
printf("\n===========================================");
printf("\n Grand Total RM %.2f",grandPrice);
printf("\n===========================================");
getch ();
}
float calculatePrice (char comboType)
{
float comboPrice, allPrice = 0.00;
int comboQty;
printf(" Enter Quantity : ");
scanf("%d", &comboQty);
if (comboType == 'A' || comboType == 'a')
{
comboPrice = 40.00 * comboQty;
}
if (comboType == 'B' || comboType == 'b')
{
comboPrice = 52.00 * comboQty;
}
if (comboType == 'C' || comboType == 'c')
{
comboPrice = 46.00 * comboQty;
}
return comboPrice;
}
It would be greatly appreciated if someone could help detecting my mistake.