This program is to calculate the price of dishes. The user needs to input the item number and it'll calculate the total price of the dish based on the size they chose. However, the 2nd switch case (the one inside the switch (item_number)) doesn't work as expected. It works only for case 1, the other cases 2-4 doesn't work, hence the program cannot display the accurate price. I absolutely cannot do this assignment using if...else, it has to be with switch case.
#include <stdio.h>
int main()
{
int item_number, dish_quantity;
char dish_size;
float price, total;
//Display the menu
printf("\t\t\t\tMenu\t\t\t\t\n");
printf("--------------------------------------------------------------------\n");
printf("Item Number Dish\t\t\t Dish Size \n");
printf("\t\t\t\tSmall(RM) Medium(RM) Large(RM)\n");
printf("--------------------------------------------------------------------\n");
printf(" 1 Ikan Tenggiri 10.00 20.00 30.00\n");
printf(" 2 Ikan Pari 8.00 15.00 25.00\n");
printf(" 3 Ikan Jenahak 9.00 17.00 28.00\n");
printf(" 4 Daging 10.00 20.00 30.00\n");
printf("--------------------------------------------------------------------\n\n");
// Input from user
printf("Enter item number : ");
scanf("%d", &item_number);
printf("Enter dish size (S/M/L) : ");
scanf("%s", &dish_size);
printf("Enter dish quantity : ");
scanf("%d", &dish_quantity);
switch (item_number){
case 1: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
case 2: switch (dish_size){
case 's': case 'S': price = 8.00; break;
case 'm': case 'M': price = 15.00; break;
case 'l': case 'L': price = 25.00; break;}
case 3: switch (dish_size){
case 's': case 'S': price = 9.00; break;
case 'm': case 'M': price = 17.00; break;
case 'l': case 'L': price = 28.00; break;}
case 4: switch (dish_size){
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;}
}
printf("Price per dish size : RM %.2f\n", price);
total = dish_quantity * price;
printf("Total charge : RM %.2f", total);
return 0;
}
The break statement will only break out of the innermost switch, but not the outer switch. Therefore, you also need break statements for the outer switch statement:
switch (item_number)
{
case 1:
switch (dish_size)
{
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
}
break;
case 2:
switch (dish_size)
{
case 's': case 'S': price = 8.00; break;
case 'm': case 'M': price = 15.00; break;
case 'l': case 'L': price = 25.00; break;
}
break;
case 3:
switch (dish_size)
{
case 's': case 'S': price = 9.00; break;
case 'm': case 'M': price = 17.00; break;
case 'l': case 'L': price = 28.00; break;
}
break;
case 4:
switch (dish_size)
{
case 's': case 'S': price = 10.00; break;
case 'm': case 'M': price = 20.00; break;
case 'l': case 'L': price = 30.00; break;
}
break;
}
Another problem is that the line
scanf("%s", &dish_size);
will cause a buffer overflow, because &dish_size only has room for storing a single character, including the terminating null character. Therefore, there is only sufficient room for storing an empty string.
For this reason, I suggest that you change that line to the following:
scanf(" %c", &dish_size);
That way, scanf will only attempt to write a single character instead of a null-terminated string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With