用字符串切换语句?

时间:2012-03-24 22:45:10

标签: c

我正在做一个小作业,我应该做一个食物菜单。无论如何,我的开关不工作。我正在尝试使用一个简单的函数,我可以传递“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");

}

5 个答案:

答案 0 :(得分:8)

C 支持这种转换,但如果,语法

 switch(choice)
 {
    case "fish":
        something();
        break;
    case "drink":
        other_thing();
        break;
 }

切换到我通常比if(else ifs)的(长)列表更清晰。即使在这种情况下它看起来过于复杂,我更喜欢这样的方法:

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

如果您选择正确的方法,您的代码可能始终相同,只有您的数据增长。

答案 1 :(得分:6)

您不能将switch语句与字符串一起使用。

您可以考虑使用strcmp来比较字符串。

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

答案 2 :(得分:6)

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

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

这比使用strcmp更快(尽管对您的情况无关紧要)且维护性较差。但是,了解所有选项并了解如何使用其中一些内容是很好的。

答案 3 :(得分:2)

C不支持字符串上的切换...您应该使用strcmp()

答案 4 :(得分:1)

switch与C中的不一样。您需要创建if语句构造并使用strcmp()来比较字符串。

相关问题