0

I have an assignment on matrix operations and I cannot figure out what is wrong in my code, that makes my program say that char is wrong, even though it is correct. Could you please help me? Thank You.

if(scanf(" %c", &symbol) == 1)              //input symbol with error handling
    {
        if (symbol != '*' || symbol != '+' || symbol != '-')
        {
            printf("[%c]\n", symbol);
            fprintf(stderr, "Error: Chybny vstup [Symbol]!\n");
            return 100;
        }
    }  

enter image description here

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Leon
  • 13
  • 3
  • Please give complete code as a [mre] as well as the exact input, expected result and actual result. Please do not post text as an image - [reasoning](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Copy it as formatted text into the question. – kaylum Dec 03 '21 at 19:51
  • 2
    `if (symbol != '*' || symbol != '+' || symbol != '-')` should have `&&` not `||`. – kaylum Dec 03 '21 at 19:52
  • 1
    Well, if you entered `'*'` then it isn't a `'+'`. Try using `&&` instead of `||`. – Weather Vane Dec 03 '21 at 19:52
  • Maybe removing space before %c works. It should become "%c" – Seyed Mohammad Amin Atyabi Dec 03 '21 at 19:53
  • 1
    @SeyedMohammadAminAtyabi the space correctly filters any leading whitespace in the input. – Weather Vane Dec 03 '21 at 19:54
  • Does this answer your question? [De Morgan's rules explained](https://stackoverflow.com/questions/2168603/de-morgans-rules-explained) – Karl Knechtel Dec 03 '21 at 19:55

1 Answers1

1

You need to use the logical AND operator && instead of the logical OR operator ||

if (symbol != '*' && symbol != '+' && symbol != '-')

Otherwise the condition of the if statement

if (symbol != '*' || symbol != '+' || symbol != '-')

will always evaluate to logical true for any character.

You could avoid the mistake if you used the negation operator the following way

if ( !( symbol == '*' || symbol == '+' || symbol == '-' ) )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335