switch语句中的C Char Switch返回case表达式不是常量

时间:2017-03-27 05:50:37

标签: c char switch-statement visual-studio-2017

enter image description here

我目前正在尝试在交换机内运行一个开关。

第一个switch语句是从用户接收一个选项,其中一个选项是“O”并显示在下面。

我收到了来自visual studio的2个错误

表达式必须是整数常量表达式
案例表达不恒定
这些错误突然出现在我正在检查案件'​​NAASA'

的行上
        case 'O':
        printf("Please enter your Company ID:");
        scanf_s("%30s", &companyIdLookup,30);
        switch (companyIdLookup[30]) {
        case 'BCFS':
            strcpy_s(companyIdLookup, 30, companyId);
            strcpy_s(companyNameLookup, 256, "Blue Fish Space Co");
            discountRateLookup = 0;
            discountTypeLookup = 0; // 0=Not applicable  1=Before Tax   2=after Tax 3=before tax if over $14,500 
            payTaxLookup = 0; // 0 = No    1=Yes
            strcpy_s(pickUpBayLookup, 30, "MERCY");
            foundCompany = 1;
            break;
        case 'ECP':
            strcpy_s(companyIdLookup, 30, companyId);
            strcpy_s(companyNameLookup, 256, "Elon Cannon Personal");
            discountRateLookup = 1.0;
            discountTypeLookup = 1; // 0=Not applicable  1=Before Tax   2=after Tax  3=before tax if over $14,500 
            payTaxLookup = 1; // 0 = No    1=Yes
            strcpy_s(pickUpBayLookup, 30, "KIT");
            foundCompany = 1;
            break;
        case 'ECF':
            strcpy_s(companyIdLookup, 30, companyId);
            strcpy_s(companyNameLookup, 256, "Eloan Credit Finance");
            discountRateLookup = 1.5;
            discountTypeLookup = 2; // 0=Not applicable  1=Before Tax   2=after Tax  3=before tax if over $14,500 
            payTaxLookup = 1; // 0 = No    1=Yes
            strcpy_s(pickUpBayLookup, 30, "MERCY");
            foundCompany = 1;
            break;
        case "NAASA"://error is here    < ----------------
            strcpy_s(companyIdLookup, 30, companyId);
            strcpy_s(companyNameLookup, 256, "NAASA");
            discountRateLookup = 0;
            discountTypeLookup = 0; // 0=Not applicable  1=Before Tax   2=after Tax  3=before tax if over $14,500 
            payTaxLookup = 1; // 0 = No    1=Yes
            strcpy_s(pickUpBayLookup, 30, "MERCY");
            foundCompany = 1;
            break;
        case 'AARG':
            strcpy_s(companyIdLookup, 30, companyId);
            strcpy_s(companyNameLookup, 256, "AARG");
            discountRateLookup = 22.5;
            discountTypeLookup = 3; // 0=Not applicable  1=Before Tax   2=after Tax  3=before tax if over $14,500  
            payTaxLookup = 1; // 0 = No    1=Yes
            strcpy_s(pickUpBayLookup, 30, "KIT");
            foundCompany = 1;
            break;
        default :
            break;

        }//End of O switch
        break;

1 个答案:

答案 0 :(得分:3)

您不能将字符串文字用作案例标签中的值。这就是MSVC在您尝试使用"NAASA"时告诉您的内容。

为什么其他人有效?因为它们不是字符串文字。它们是字符常量(不是字符串!),具有实现定义的含义。

6.4.4.4 Character constants

  

2整数字符常量是用单引号括起来的一个或多个多字节字符的序列,如'x'。
  ...
  10整数字符常量的类型为int。包含映射到单字节执行字符的单个字符的整数字符常量的值是解释为整数的映射字符的表示的数值。包含多个字符(例如,'ab')的整数字符常量的值,或包含未映射到单字节执行字符的字符或转义序列的值是实现定义的。

单引号和双引号在C中不可互换,因为它们可能在其他一些语言中。

如果要分支字符串的值,则需要先将其转换为整数。例如:

struct {
  char const * str;
  int          num;
} branch[] = {
  { "O",     0 },
  { "BCFS",  1 },
  { "NAASA", 2 },
  // etc
}

int num = -1;
for (int i = 0; i < sizeof(branch)/sizeof(branch[0]); ++i)
  if (strcmp(input, branch[i].str) == 0) {
    num = branch[i].num; break;
  }

switch(num) {
default:
  perror("not at a valid option");
  break;
case 0:
  // other things
case 1:
}

或使用一系列if语句:

if(strcmp(input, "O") == 0) {

} else if(strcmp(input, "BCFS") == 0) {

} else if(/*etc*/) {

}