Possible Duplicate:
How do I select a range of values in a switch statement?
I’ve been getting some errors, and I’ve been searching for some time now, but I have no idea what is the cause of the errors. (I’m quite new to programming.)
Here are the errors I’m getting:
error: 'Essais' cannot appear in a constant-expression| (line 200)
warning: overflow in implicit constant conversion| (line 202)
I have case and cote:
char AfficherCote (int Essais)
{
char Cote;
switch (Essais)
{
(line200) case Essais<=20:
{
(line 202) Cote='Excellent';
return (Cote);
break;
}
case Essais<=40:
{
Cote='Très bon';
return (Cote);
break;
}
case Essais<=60:
{
Cote='Bon';
return (Cote);
break;
}
case Essais<=80:
{
Cote='Moyen';
return (Cote);
break;
}
case Essais<=100:
{
Cote='Muvais';
return (Cote);
break;
}
case Essais>=100:
{
Cote='Très mauvais';
return (Cote);
}
}
}
switch-caseonly works with constant values(*) (such as3or'a'), not with ranges (such as<=100). You also must not include the variable name in thecasestatement. Correct syntax would be as follows:If you need range tests, use
ifinstead ofswitch-case:Also note that you can’t use single quotation marks
'for strings. Use double quotation marks"instead, and use variables of typestd::string(notchar) to store strings.(*) To be precise, the condition given in the
casestatements must be a constant expression of integral type, enumeration type, or class type convertible to integer or enumeration type (see §6.4.2/2 of the C++ Standard for details).