Reputation: 27
Hi so i'm new at coding in C and i'd like to know how to compare several variables. Cause my if statement is only working for the first variable and ignore the ||.
scanf("%d %c %d", &nbsaisi, &op, &nbsaisi2);
if((op != multi) || (op != plus) || (op != moins) || (op!= divi))
{
printf("You haven't entered a valid operator.\n");
exit(1);
}
Upvotes: 1
Views: 985
Reputation: 412
You should enter inside the if block only if all the conditions are met, so in your case your conditions should be in &&
and not in ||
if((op != multi) && (op != plus) && (op != moins) && (op!= divi))
{
printf("You haven't entered a valid operator.\n");
exit(1);
}
Upvotes: 1