C Char Switch inside a switch statement returns case expression not constant

C Char Switch inside a switch statement returns case expression not constant

我目前正在尝试 运行 开关内的开关。

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

我从 visual studio

收到 2 个错误

表达式必须是整数常量表达式
case 表达式不是常量
这些错误在我检查案例的行中弹出 '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 ,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 ,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 ,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 ,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 ,500  
            payTaxLookup = 1; // 0 = No    1=Yes
            strcpy_s(pickUpBayLookup, 30, "KIT");
            foundCompany = 1;
            break;
        default :
            break;

        }//End of O switch
        break;

您不能使用字符串文字作为大小写标签中的值。这就是 MSVC 在您尝试使用 "NAASA".

时告诉您的内容

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

6.4.4.4 Character constants

2 An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.
...
10 An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

单引号和双引号在 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*/) {

}