Switch statement multi character constant

I am trying to convert this to a switch statement

if (codeSection == 281) cout << "bigamy"; else if (codeSection == 321 || codeSection == 322) cout << "selling illegal lottery tickets"; else if (codeSection == 383) cout << "selling rancid butter"; else if (codeSection == 598) cout << "wounding a bird in a public cemetery"; else cout << "some other crime"; // Actual switch statement switch (codeSection) { case '281': cout << "bigamy" << endl; break; case '321': case '322': cout << "selling illegal lottery tickets" << endl; break; case '383': cout << "selling rancid butter" << endl; break; case '598': cout << "wounding a bird in a public cemetery"; break; default: cout << "some other crime"<< endl; } 

The compiler talks about the constant of the multi character operator and gives a yellow warning, but is still compiling. My question is should it only be in char form? as the case of '2'

+5
source share
1 answer
 case '281': 

it should be

 case 281: 

and similarly for the rest, otherwise the compiler "thinks" that you are trying to use a multi-character constant, which you most likely do not want.

A case should not be a char . In fact, it should be a constant expression of the same type as the condition type after conversions and integral promotions, see, for example, http://en.cppreference.com/w/cpp/language/switch .

+5
source

Source: https://habr.com/ru/post/1233820/


All Articles