切换大小写提示默认为“A”,忽略 C 编码中的“B”

Switch Case Prompts default to 'A' and ignore 'B' in C Coding

提问人:ThePhycho 提问时间:1/10/2023 最后编辑:Michael M.ThePhycho 更新时间:1/17/2023 访问量:44

问:

只是我代码中的一个错误,我正在编写一个拒绝合作的汉堡菜单。请帮助我理解为什么在下面的情况下,即使我输入它默认为?我在格式上搞砸了吗?'B''A'

#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 认为这可能是语法类型格式错误,但我不确定。 谢谢!

预计开关盒能够正确通过菜单。

c switch-语句

评论


答:

2赞 Leaderboard 1/10/2023 #1

在您的代码中,我没有看到您正在打开,这是实际更改为相应汉堡菜单所必需的。因此,您需要做的就是创建一个使用 BurgerChoice 的内部 switch 语句,如下所示:BurgerChoice

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;    
          }
      }

代码现在似乎可以作为结果工作(除了可以修复的菜单的重复打印):

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:

评论

0赞 ThePhycho 1/10/2023
谢谢伙计!用于识别问题!所以它更像是一个混乱的嵌套开关盒,对吧?其次,我已经对代码进行了调整以包含辅助开关,但是它一直只选择鸡肉,并且不会更新选择 printf。有什么想法吗?对不起,如果我很痛苦
0赞 Leaderboard 1/12/2023
我看不到您代码的更新版本。