简体   繁体   中英

C-Comparison with multiple variables in a if statement

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); 
   }

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); 
   }

You should use && instead of ||

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM