-3
void addItem(char selection) {
double total = 0.0;
qty = 0;
cout \<\< "Enter Qty: ";
cin \>\> qty;

    `else if (selection == '10') {
        total = 60 * qty;
        cout << "Added to order"<<endl;
        Iattotal += total;
        Iatqty += qty;
        Gtotal += Iattotal;
    }
}


main.cpp:93:22: warning: multi-character character constant \[-Wmultichar\]
93 |     if (selection == '10') {
|                      ^\~\~\~
                    ^\~\~\~

It's there any solution for this peoblem I tried to change char to int but it didn't changed and i tried to find similar problems but it didn't fixed

hush
  • 1
  • 1
  • Multi-character literals like `'10'` are implementation defined in their behavior. Either use single-character literals, strings, or just plain `int` values to begin with. And remember to change the type of your variables as well, so `char selection` should probably be `int selection`. – Some programmer dude Dec 12 '22 at 06:01
  • What is it `\<\<`, does it compile? – 273K Dec 12 '22 at 06:02

2 Answers2

0

Depends on what you want to do:

to read the value as an ascii code, you can write

char a = 'a';
int ia = (int)a; 

/* note that the int cast is not necessary -- int ia = a would suffice */ to convert the character '0' -> 0, '1' -> 1, etc, you can write

char a = '4';
int ia = a - '0';

/* check here if ia is bounded by 0 and 9 */

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '22 at 20:33
0

The error is being produced because you are comparing a the "selection" variable, which is a char type, and can hold only one character, to '10' which is a multi-character constant.

If you are trying to pass a multi character variable, such as user input, to the function you might consider using a std:string.

Std::string can hold multiple characters, and can be compared using the == operator.

Alternatively, you could change the type to char * - this is how c-strings are normally defined. C-Strings are null terminated to denote the end of the string.

If you are using a c-string, you would need to use a library function like strcmp to compare the value to the constant. strcmp returns 0 when the two strings are equal.

When dealing with c-strings you have to be careful not to read beyond the size of the buffer or array. For safety it is better to use strncmp, where the maximum number of characters to be compared can be specified.