Reputation: 51
I'm trying to convert this into 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 says switch statement multi character constant, and gives me a yellow warning but still compiles. My question is are the case supposed to be only in char form? like case '2'
Upvotes: 5
Views: 3612
Reputation: 56577
case '281':
should be
case 281:
and similarly for the rest, otherwise the compiler "thinks" that you try to use a multi-character constant, which is not what you probably want.
A case
doesn't have to be a char
. In fact, it must be a constant expression of the same type as the type of condition after conversions and integral promotions, see e.g. http://en.cppreference.com/w/cpp/language/switch.
Upvotes: 6