简体   繁体   中英

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.

i am receiving 2 errors from visual studio

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'

        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" .

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

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 (eg, 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

Single and double quotes aren't interchangeable in C as they may be in some other languages.

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(strcmp(input, "O") == 0) {

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

} else if(/*etc*/) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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