EDITION: In view of the more than correct objections indicated by the user Paula_Plus_Plus I have decided to delete the first paragraph and a half of my answer because it was incorrect.
Returning to the question, in C ++ (unlike C # for example), you can not put any type of variable in case
s, only int
or convertible types (unequivocally) to int
as char
, those that derive from enum
s, etc. For more information you can review the following links (the latter in English).
So one way to solve your problem would be to use the initials of the variables that you will put in each case
, that is, work with char
s. For example, instead of case 'Coke':
you can put case 'C':
.
The possible problem with the above is that it is not entirely clear that it can mean the C
, even more so there may also be another variable that starts with the letter C and then we get into a little mess. In this case, my alternative would be to use an enumeration. More specifically, I would do the following:
enum productos_tienda { Coke = 1, Sabritas, Pepsi, etc};
In this way, you can now write case Coke:
and so on with every product you have. If you do not know about the enumerations in C ++, I leave you these dos links (the first in English).
On the other hand, while it is true that a warning is not the same as an error, the first one indicates that there is something "strange" in your code or that it does not make much sense for the compiler (which can mean code smell ); and that's why I disagree with the suggestion to disable the warning that is indicated in your question by the option -Wno-multichar
. While there may be specific cases in which ignoring warning
s is the best solution, I think you can always write free code of any kind of warnings. It is more advisable that when compiling you tell the compiler to be as strict as possible with the code you have written.
For this purpose you can include the following options as compile parameters: -Wall -Wextra -pedantic
, or even if you want to be more "radical", you can make the compiler mark you any warning as an error. Usually the option -Werror
will serve for that purpose.