Home > Blockchain >  I always get the same if
I always get the same if

Time:11-28

int main(int argc, char *argv[]) {
    int hacim,yas;
    char ad[20]; //Kullanıcı adı 
    printf("Kullanici adinizi girin: \n");
    scanf("%s", ad);
    printf("Sifrenizi girin: \n");
    char sifre[20]; // Şifre
    scanf("%s", sifre);
    
        if (strcmp(ad,"ahmet")==0 && strcmp(sifre,"1234")==0){
         printf("Giris basarili. Hosgeldiniz\n\n");
        }
        else
        {
         printf("Hatali giris yaptiniz \n");
        }
    
    
    printf("Aracin motor hacmini giriniz:");
    scanf("%d",&hacim);
    printf("\nAracin model yilini giriniz:");
    scanf("%d",&yas);
            if(1<=yas<=3 && 0<=hacim<=1300){
                printf("\nOdemeniz gereken tutar 646TL");
             }
            else if(1<=yas<=3 && 1301<=hacim<=1600){
                printf("Odemeniz gereken tutar 1035TL");
             }
            else if(4<=yas<=6 && 0<=hacim<=1300){
                printf("Odemeniz gereken tutar 450TL");
             }
            else if(4<=yas<=6 && 1300<=hacim<=1600){
                printf("Odemeniz gereken tutar 776TL");
             }      
            else{
                printf("yanlis deger");
            }
return 0;
}

i always get 646 even with different values. Always one if works. Help me? Also what is wrong with this damn thing maybe i can explain myself with two sentences chill.

CodePudding user response:

1<=yas<=3

In C this expression is always true. Why? It can be written as (1<=yas)<=3

1<=yas can be 0 or 1. Both of those values are smaller than 3.

You need to use logical expression to have more complex number comparisons:

if(yas >= 1 && yas <= 3)

You need to change all simialt expressions in your program.

CodePudding user response:

0<=hacim<=1300 This expression performs the first relational operation because of the precedence of the operator,it's obviously going to be 1.And then you compare 1 to 1300, and obviously you still get 1.So as long as hacim is greater than 0, it's going to be 1. That's what went wrong.

  •  Tags:  
  • c
  • Related