带字符串的Switch语句?

带字符串的Switch语句?,c,C,我正在做一个小作业,我要做一份食物菜单。不管怎样,我的开关坏了。我尝试使用一个简单的函数,我可以将“fish”、“drink”或“chips”的值传递给它,然后它将输出: "Are you ordering FISH?" (or chips/drink) 我无法让开关工作,它应该检测我传入它的内容,然后根据开关的情况输出printf 代码: #include <stdio.h> void menu() { printf("\nWelcome to Sunny FISH

我正在做一个小作业,我要做一份食物菜单。不管怎样,我的开关坏了。我尝试使用一个简单的函数,我可以将“fish”、“drink”或“chips”的值传递给它,然后它将输出:

"Are you ordering FISH?" (or chips/drink)  
我无法让开关工作,它应该检测我传入它的内容,然后根据开关的情况输出printf

代码:

#include <stdio.h>

void menu() {
    printf("\nWelcome to Sunny FISH & CHIPS!\n\n");
    printf("########     Fish :     Haddock(K) Large(L) | $5.00\n");
    printf("# FOOD #                Halibut(T) Large(L) | $4.00\n");
    printf("########     Chips:     Cut(C)     Large(L) | $2.00\n");
    printf("                        Ring(R)    Large(L) | $3.00\n");
    printf("                                            | \n");
    printf("##########   Soft Drinks(S)        Large(L) | $2.00\n");
    printf("# DRINKS #   Coffee(C)             Large(L) | $1.75\n");
    printf("##########   Tea(T)                Large(L) | $1.50\n");
    printf("---------------------------------------------\n");
    printf("Note: Medium price: 80%% of large.\n");
    printf("       Small price: 60%% of large.\n");
    printf("TAX is 10%%.\n");
    printf("More than 5 fish, 10%% discount on drink.\n");
    printf("Every 10 fish purchased, get 1 free softdrink.\n");
    printf("  - size of drink is according to size of fish\n");
}

void question (char choice[5]) {
    switch (choice[5]) 
    {
        case choice["fish"]:
            printf("Do you order FISH?\n");
        case choice["drink"]:
            printf("Do you order CHIPS?\n");
        case choice["chips"] :
            printf("Do you order DRINKS?\n");
        default :
            printf("Enter a valid choice: \n");
    }
}

main() {

    // menu();
    question("fish");

}
#包括
无效菜单(){
printf(“\n欢迎光临阳光鱼和薯条!\n\n”);
printf(“##########鱼:黑线鳕(K)大型(L)|$5.00\n”);
printf(“#食品#大比目鱼(T)L)|$4.00\n”);
printf(“###########切屑:切(C)大(L)| 2.00美元\n”);
printf(“环(R)大(L)|$3.00\n”);
printf(“|\n”);
printf(“大容量软饮料(L)$2.00\n”);
printf(“#饮料#咖啡(C)大杯(L)| 1.75美元”);
printf(“##############################;
printf(“-------------------------------------------------------------\n”);
printf(“注:中等价格:大件的80%。\n”);
printf(“小价格:大价格的60%。\n”);
printf(“税为10%。\n”);
printf(“超过5条鱼,饮料10%%折扣。\n”);
printf(“每购买10条鱼,可免费获得1杯软饮料。\n”);
printf(“-饮料的大小取决于鱼的大小\n”);
}
无效问题(字符选择[5]){
开关(选项[5])
{
案例选择[“鱼”]:
printf(“您点鱼吗?\n”);
案例选择[“饮料”]:
printf(“您订购芯片吗?\n”);
案例选择[“芯片”]:
printf(“您点饮料吗?\n”);
违约:
printf(“输入有效选项:\n”);
}
}
main(){
//菜单();
问题(“鱼”);
}

C不支持字符串上的开关…您应该使用
strcmp()
switch
与C中的工作方式不同。如果
语句构造并使用
strcmp()
来比较字符串,则需要使用
switch
语句

if (strcmp(choice,"fish")==0) {
     //fish
} 
else if (strcmp(choice,"drink")==0) {
     //drink
}
. 
.
.

您可以考虑使用<代码> STRCMP <代码>来比较字符串。

if (strcmp(choice,"fish")==0) {
     //fish
} 
else if (strcmp(choice,"drink")==0) {
     //drink
}
. 
.
.

除其他答案外,如果您发现您的选择列表都以一个唯一的字母开头(或在另一个位置有一个唯一的字母),那么您可以
切换该字母:

switch (choice[0]) {
case 'f':
    // they chose fish
    break;
case 'c':
    // they chose chips
    break;
case 'd':
    // they chose drink
}

这将比使用strcmp快(尽管这对您的情况并不重要),而且维护性较差。但是,了解所有选项并了解如何使用其中一些东西是很好的。

C不支持这种切换,但是如果它支持,语法将是

 switch(choice)
 {
    case "fish":
        something();
        break;
    case "drink":
        other_thing();
        break;
 }
切换到“我”通常比一长串“如果-否则”如果更清楚。尽管在这种情况下似乎过于复杂,但我更喜欢这样的方法:

#include <stdio.h>
#include <string.h>

enum menu_items { FISH, DRINK, CHIPS, UNKNOWN };

struct items
{
  char *name;
  enum menu_items id;
} items_list[] = {
  { "fish", FISH },
  { "drink", DRINK },
  { "chips", CHIPS }
};

int main(void)
{
  int i;
  enum menu_items mid;
  struct items *choice = NULL;

  // ...

  for(i = 0, choice = NULL; i < sizeof items_list/sizeof (struct items); i++)
  {
    if (strcmp(answer, items_list[i].name) == 0)
    {
      choice = items_list + i;
      break;
    }
  }    

  mid = choice ? choice->id : UNKNOWN;

  // the following would be enough to obtain the output of your example;
  // I've not embodied the code into a func, but it's easy to do if you need
  if ( mid != UNKNOWN )
  {
      // the function a_func transforms the string of the food
      // e.g. to uppercase, or it could map it to whatever according to some
      // other data... or expand the struct to hold what you want to output
      // with "fish", "drink", "chips", e.g. choice->screen_name
      printf("Do you order %s?\n", a_func(choice->name));
  }
  else
  {
      printf("Enter a valid choice:\n");
  }
  // ---------

  // or if you prefer the switch you have something like:

  switch(mid)
  {
  case FISH:
    printf("fish\n");
    break;
  case DRINK:
    printf("drink\n");
    break;
  case CHIPS:
    printf("chips\n");
    break;
  default:
    printf("unknown choice\n");
    break;
  }

  return 0;
}
#包括
#包括
列举菜单项{鱼、饮料、薯条、未知};
结构项
{
字符*名称;
枚举菜单项id;
}项目列表[]={
{“鱼”,鱼},
{“喝”,喝},
{“芯片”,芯片}
};
内部主(空)
{
int i;
枚举菜单项;
结构项*choice=NULL;
// ...
对于(i=0,choice=NULL;iid:未知;
//以下内容足以获得示例的输出;
//我还没有将代码具体化到func中,但是如果需要的话,它很容易实现
如果(中间!=未知)
{
//函数a_func转换食物字符串
//例如,使用大写字母,或者它可以根据某些规则将其映射到任何内容
//其他数据…或展开结构以保存要输出的内容
//带有“鱼”、“饮料”、“薯条”,例如选择->屏幕名称
printf(“您订购%s吗?\n”,a_func(选择->名称));
}
其他的
{
printf(“输入有效选项:\n”);
}
// ---------
//或者,如果您更喜欢开关,您可以选择:
开关(mid)
{
案例鱼:
printf(“鱼”);
打破
酒类:
printf(“饮料”);
打破
机箱芯片:
printf(“chips\n”);
打破
违约:
printf(“未知选择”);
打破
}
返回0;
}

如果选择正确的方法,代码可能总是相同的,只有数据会增长。

在每个案例中,您应该使用
strcmp
来确保完整字符串实际匹配。@R。。正如我所说的,只有当每个选项在某个位置上都有一个唯一的字母时,这才有效。这是一个单独的问题。我的意思是,您的解决方案还将接受以相同字母开头但不在列表中的其他字符串。@R。。是的,这是真的,我的回答假设输入是有效的。我在评论中的目的是指出,即使你不能预先假定,你的答案仍然是好的,只要你在案例中添加
strcmp
检查(这仍然比用
strcmp
对每个案例进行线性搜索更有效)。