Reputation: 11
Just a bug in my code that I'm writing in regards to a burger menu that refuses to cooperate. Please help me understand why in the case below Even if I input 'B'
it defaults to 'A'
? Did I mess up in formatting?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum burger_types {
Chicken, Ham, Vegie
};
struct Burger {
char receiptNumber[7];
enum burger_types BurgerType;
int quantity;
float totalPrice;
};
int main () {
int quantity;
float ChickenPrice = 15.0, HamPrice = 17.0, VegiePrice = 13, totalPrice, burgerDiscount = 0.9, burgerDiscountOffset = 0.1, discountTotal;
int choice;
const int MAX_RECEIPTS = 25;
struct Burger receipts[MAX_RECEIPTS];
int numReceipts = 0; // Number of receipts stored in the array
do
{
// Print menu
printf ("Menu:\n");
printf ("1. Order Burger\n");
printf ("2. Cancel Order\n");
printf ("3. Exit\n");
printf ("Enter your choice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
// Order burger
{
char BurgerChoice;
printf ("Select the Burger Type\n ");
printf ("A – Chicken Burger – $15\n ");
printf ("B – Ham Burger – $17\n ");
printf ("C – Vegie Burger – $13\n ");
printf ("X – To return to Main Menu\n ");
scanf ("%c", &BurgerChoice);
getchar ();
case 'A':
// Chicken Burger
{
printf ("How many Chicken burgers would you like to order?\n ");
scanf ("%d", &quantity);
// Calculate total price and apply discount if necessary
if (quantity >= 5) {
totalPrice = quantity * ChickenPrice;
burgerDiscount = quantity * ChickenPrice * burgerDiscountOffset;
discountTotal = totalPrice - burgerDiscount;
} else {
totalPrice = quantity * ChickenPrice;
}
if (numReceipts < MAX_RECEIPTS) {
// Store receipt in array
snprintf (receipts[numReceipts].receiptNumber, 7, "B%04d", numReceipts + 1); // Assign string value
receipts[numReceipts].BurgerType = Chicken;
receipts[numReceipts].quantity = quantity;
receipts[numReceipts].totalPrice = totalPrice;
numReceipts++;
} else {
printf ("Cannot generate more receipts. Array is full.\n");
break;
}
printf ("Your total bill value is $%.2f.\n", receipts[numReceipts - 1].totalPrice);
if (quantity >= 5) {
printf ("Discount 10%% - $%.2f\n", burgerDiscount);
printf ("Final Bill Value is $%.2f\n", discountTotal);
}
printf ("Your Receipt number is %s.\n",receipts[numReceipts - 1].receiptNumber);
printf ("Please go to a register and make the payment by quoting the Receipt Number - %s:\n",receipts[numReceipts - 1].receiptNumber);
// Prompt user to press any key to continue
getchar (); // Read and discard character from keyboard
printf ("\n<<Press any key to continue>>\n");
getchar ();
}
break;
case 'B':
// Chicken Burger
{
printf ("How many Ham burgers would you like to order?\n ");
scanf ("%d", &quantity);
// Calculate total price and apply discount if necessary
if (quantity >= 5) {
totalPrice = quantity * HamPrice;
burgerDiscount = quantity * HamPrice * burgerDiscountOffset;
discountTotal = totalPrice - burgerDiscount;
} else {
totalPrice = quantity * HamPrice;
}
if (numReceipts < MAX_RECEIPTS) {
// Store receipt in array
snprintf (receipts[numReceipts].receiptNumber, 7, "B%04d", numReceipts + 1); // Assign string value
receipts[numReceipts].BurgerType = Ham;
receipts[numReceipts].quantity = quantity;
receipts[numReceipts].totalPrice = totalPrice;
numReceipts++;
} else {
printf ("Cannot generate more receipts. Array is full.\n");
break;
}
printf ("Your total bill value is $%.2f.\n", receipts[numReceipts - 1].totalPrice);
if (quantity >= 5) {
printf ("Discount 10%% - $%.2f\n", burgerDiscount);
printf ("Final Bill Value is $%.2f\n", discountTotal);
}
printf ("Your Receipt number is %s.\n",receipts[numReceipts - 1].receiptNumber);
printf ("Please go to a register and make the payment by quoting the Receipt Number - %s:\n",receipts[numReceipts - 1].receiptNumber);
// Prompt user to press any key to continue
getchar (); // Read and discard character from keyboard
printf ("\n<<Press any key to continue>>\n");
getchar ();
}
break;
}
break;
case 2:
break;
case 3:
// Exit
printf ("Goodbye!\n");
break;
default:
printf ("Invalid choice.\n");
break;
}
}
while (choice != 3);
return 0;
}
IO think it might be a syntax type formatting error but I'm not sure. Thanks!
Expected the switch case to go through the menu correctly.
Upvotes: 0
Views: 48
Reputation: 391
Nowhere in your code do I see that you are switching on BurgerChoice
, which is needed to actually change to the respective burger menu. Hence all you need to do is to create an inner switch statement that makes use of BurgerChoice, like the one below:
switch (choice)
{
case 1:
// Order burger
{
char BurgerChoice;
printf ("Select the Burger Type\n ");
printf ("A – Chicken Burger – $15\n ");
printf ("B – Ham Burger – $17\n ");
printf ("C – Vegie Burger – $13\n ");
printf ("X – To return to Main Menu\n ");
scanf ("%c", &BurgerChoice);
// getchar ();
printf("Choice taken = %c", BurgerChoice);
switch(BurgerChoice)
{
case 'A':
// Chicken Burger
{
printf ("How many Chicken burgers would you like to order?\n ");
scanf ("%d", &quantity);
// Calculate total price and apply discount if necessary
if (quantity >= 5) {
totalPrice = quantity * ChickenPrice;
burgerDiscount = quantity * ChickenPrice * burgerDiscountOffset;
discountTotal = totalPrice - burgerDiscount;
} else {
totalPrice = quantity * ChickenPrice;
}
if (numReceipts < MAX_RECEIPTS) {
// Store receipt in array
snprintf (receipts[numReceipts].receiptNumber, 7, "B%04d", numReceipts + 1); // Assign string value
receipts[numReceipts].BurgerType = Chicken;
receipts[numReceipts].quantity = quantity;
receipts[numReceipts].totalPrice = totalPrice;
numReceipts++;
} else {
printf ("Cannot generate more receipts. Array is full.\n");
break;
}
printf ("Your total bill value is $%.2f.\n", receipts[numReceipts - 1].totalPrice);
if (quantity >= 5) {
printf ("Discount 10%% - $%.2f\n", burgerDiscount);
printf ("Final Bill Value is $%.2f\n", discountTotal);
}
printf ("Your Receipt number is %s.\n",receipts[numReceipts - 1].receiptNumber);
printf ("Please go to a register and make the payment by quoting the Receipt Number - %s:\n",receipts[numReceipts - 1].receiptNumber);
// Prompt user to press any key to continue
getchar (); // Read and discard character from keyboard
printf ("\n<<Press any key to continue>>\n");
getchar ();
}
break;
case 'B':
// Chicken Burger
{
printf ("How many Ham burgers would you like to order?\n ");
scanf ("%d", &quantity);
// Calculate total price and apply discount if necessary
if (quantity >= 5) {
totalPrice = quantity * HamPrice;
burgerDiscount = quantity * HamPrice * burgerDiscountOffset;
discountTotal = totalPrice - burgerDiscount;
} else {
totalPrice = quantity * HamPrice;
}
if (numReceipts < MAX_RECEIPTS) {
// Store receipt in array
snprintf (receipts[numReceipts].receiptNumber, 7, "B%04d", numReceipts + 1); // Assign string value
receipts[numReceipts].BurgerType = Ham;
receipts[numReceipts].quantity = quantity;
receipts[numReceipts].totalPrice = totalPrice;
numReceipts++;
} else {
printf ("Cannot generate more receipts. Array is full.\n");
break;
}
printf ("Your total bill value is $%.2f.\n", receipts[numReceipts - 1].totalPrice);
if (quantity >= 5) {
printf ("Discount 10%% - $%.2f\n", burgerDiscount);
printf ("Final Bill Value is $%.2f\n", discountTotal);
}
printf ("Your Receipt number is %s.\n",receipts[numReceipts - 1].receiptNumber);
printf ("Please go to a register and make the payment by quoting the Receipt Number - %s:\n",receipts[numReceipts - 1].receiptNumber);
// Prompt user to press any key to continue
getchar (); // Read and discard character from keyboard
printf ("\n<<Press any key to continue>>\n");
getchar ();
}
break;
}
}
The code now seems to work as a result (barring the repeated printing of menu which can be fixed):
Menu:
1. Order Burger
2. Cancel Order
3. Exit
Enter your choice: 1
Select the Burger Type
A – Chicken Burger – $15
B – Ham Burger – $17
C – Vegie Burger – $13
X – To return to Main Menu
Menu:
1. Order Burger
2. Cancel Order
3. Exit
Enter your choice: B
Select the Burger Type
A – Chicken Burger – $15
B – Ham Burger – $17
C – Vegie Burger – $13
X – To return to Main Menu
How many Ham burgers would you like to order?
3
Your total bill value is $51.00.
Your Receipt number is B0001.
Please go to a register and make the payment by quoting the Receipt Number - B0001:
Upvotes: 2