How to fix the error [Warning] multi-character character constant [-Wmultichar] in C ++

5

I'm starting to make a program in C ++ from a store, but it marks the error [Warning] multi-character character constant [-Wmultichar] in the part that I marked with *** :

main ()
{
    int Coke, Sabritas, Pepsi, Gamesa, marca, Marinela, TRosa;
    printf ("Selecciona la marca de la cual quieres comprar un producto: \n 1: Coca Cola\n 2: Sabritas \n \n 3: Pepsi \n \n 4: Gamesa \n \n Marinela \n \n Tia Rosa \n");

    scanf ("%d", &marca);
    switch (marca) {
    ***case 'Coke':***
        printf ("Elija el producto: \n 1: Coca Cola \n \n 2: Coca Cola Light \n \n 3: Coca Cola Sin Azucar \n \n 4: Sprite \n \n 5: Fanta \n \n 6: Ciel \n \n 7: Sidral Mundet \n \n 8: Del Valle \n \n 9: Fresca \n \n 10: Fuze Tea \n");

system ("pause"); }
return 0;
}
    
asked by Daniiela Garc. C 23.03.2018 в 04:18
source

3 answers

5

C ++ hides dark and unknown features; you've run into one of them.

Multicarácter literals.

When you enclose a string in single quotes, the compiler interprets it as a whole number. The C ++ standard refers to these values in the section 5.13.3.2 (highlighting and my translation):

  

5.13.3 Character literals

     
  • [...]
  •   
  • A character literal that does not start with u8 , u , U or L is a literal of ordinary character . An ordinary character literal containing a single character that can be represented by the collection of execution characters has type char , with a value equivalent to the numeric value of the encoding of that character in the collection of execution characters. An ordinary character literal that contains more than one character is a multi-character literal . A multi-character literal or an ordinary character literal with a single character that can not be represented by the collection of execution characters, has conditional support, has type int and has a value dependent on implementation .
  •   

    The compiler warns you with an alarm (not an error) that you are using a feature that depends on implementation (that is, it can work differently in other compilers).

    How do I correct the error?

    It is not an error, because it allows you to compile (and it is marked as an alarm). But if you do not want to appear, you must stop using multi-character literals or disable the alarm Wmultichar in the configuration of your compiler; depends on the compiler you use will be done one way or another but in general it should be enough to add -Wno-multichar as a compilation parameter.

    Other things to consider.

    • You are programming in C ++, do not use C utilities. Change your printf and scanf by std::cin and std::cout .
    • The main function must have int as the return type.
    • It seems that you are confusing the names of variables with the value entered by the user; They are independent things. I advise you to use an enumerated one, assign 1 to the first value of it if you want to keep the indexing that you show in your code.

    Taking into account the previous points, your code could look like this:

    int main()
    {
        enum bebida { Coke = 1, Sabritas, Pepsi, Gamesa, Marinela, TRosa };
    
        std::cout << "Selecciona la marca de la cual quieres comprar un producto:\n"
                    "1: Coca Cola\n"
                    "2: Sabritas\n"
                    "3: Pepsi\n"
                    "4: Gamesa\n"
                    "5: Marinela\n"
                    "6: Tia Rosa \n";
    
        int marca;
        std::cin >> marca;
    
        switch (marca) {
            case Coke:
            break;
            case Sabritas:
            break;
            case Pepsi:
            break;
            case Gamesa:
            break;
            case Marinela:
            break;
            case TRosa:
            break;
        }
        return 0;
    }
    
        
    answered by 23.03.2018 в 07:51
    1

    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.

        
    answered by 23.03.2018 в 05:36
    0

    To use a switch case in c ++ is simple, you just have to use the options menu so that you capture the correct data and open the option you want to select. In your case you want to save Coke as a parameter but you would not be saving it in that way from your capture-imprecion, what you do in your code is to save a number, I say that you select the cocacola that is what you want to match to coke so that you open the first option so for that you should make another switch case where option 1 is equal to the coke character string or directly save that character string from your data capture by having the user place it directly.

    I propose to you that it is better to use switch case in the case of your menu, specifically use a number structure and it will be easier for you. Example:

    main ()
    {
      int op;
      printf ("Selecciona la marca de la cual quieres comprar un producto: \n 1: Coca Cola\n\n 2: Sabritas \n \n 3: Pepsi \n \n 4: Gamesa \n \n 5: Marinela \n \n 6: Tia Rosa \n");
      scanf ("%d", &op);
      switch (op)
        {
        case 1:
          printf ("Elija el producto: \n 1: Coca Cola \n \n 2: Coca Cola Light \n \n 3: Coca Cola Sin Azucar \n \n 4: Sprite \n \n 5: Fanta \n \n 6: Ciel \n \n 7: Sidral Mundet \n \n 8: Del Valle \n \n 9: Fresca \n \n 10: Fuze Tea \n");
          system ("pause");
        }
    }
    
        
    answered by 23.03.2018 в 05:30