Using the code
sscanf(argv[1], "%d", &num1);
sscanf(argv[2], "%c", &op);
sscanf(argv[3], "%d", &num2);
if ((op != '-')||(op != '*')||(op != '/')||(op != '+'))
{
puts("Error:");
printf("'%c' is not a valid operator", op);
return 0;
}
Compiles fine.
However, does not work for any input. For instance when the input is 4 + 7
Console prints:
Error:
‘+’ is not a valid operator
Edit: without this error checking code, the rest of the programme operates correctly, including a switch statement based on op!
Makes sense, since your condition is always true.
or even
will always evaluate to
true.You’re basically saying “If
opis not-ORopis not*… do whatever”.You probably meant to use
&&instead.