简体   繁体   English

在switch语句中的C Char Switch返回大小写表达式不是常量

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

在此处输入图片说明

I am currently trying to run a switch inside a switch. 我目前正在尝试在交换机内部运行交换机。

the first switch statement is to receive a option from the user and one of the options is "O" and displayed below. 第一个switch语句是从用户那里接收一个选项,并且其中一个选项是“ O”,并显示在下面。

i am receiving 2 errors from visual studio 我从Visual Studio收到2个错误

expression must be an integral constant expression 表达式必须是整数常量表达式
case expression not constant 情况表达不恒定
These errors are popping up on the line where i'm checking the case 'NAASA' 这些错误在我正在检查案例“ 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;

You can't use string literals as the values in case labels. 您不能将字符串文字用作大小写标签中的值。 That's what MSVC is telling you when you try to use "NAASA" . 这就是MSVC在您尝试使用"NAASA"时会告诉您的内容。

Why do the others work? 其他人为什么工作? Because they aren't string literals. 因为它们不是字符串文字。 They are character constants (not strings!), with implementation defined meaning. 它们是字符常量(不是字符串!),具有实现定义的含义。

6.4.4.4 Character constants 6.4.4.4字符常量

2 An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. 2整数字符常量是由单引号引起的一个或多个多字节字符的序列,例如'x'。
... ...
10 An integer character constant has type int. 10整数字符常量的类型为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 (eg, 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. 包含多个字符(例如,“ ab”)或不映射到单字节执行字符的字符或转义序列的整数字符常量的值是实现定义的。

Single and double quotes aren't interchangeable in C as they may be in some other languages. 单引号和双引号在C语言中是不可互换的,因为在某些其他语言中也是如此。

If you want to branch on the value of a string, you need to convert it to an integer first. 如果要分支到字符串的值,则需要先将其转换为整数。 For instance: 例如:

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

Or use a chain of if statements: 或使用一系列if语句:

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

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

} else if(/*etc*/) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM