vendredi 19 février 2021

Why this if accepts all conditions, even ones it shouldn't?

Afternoon, I have a question that asks me to check if multiple inputted lengths are smaller than an original base value (20x20x20) and tell case by case if they would fit inside of the base value or not, I tried it as follows:

#include<stdio.h>
#include<string.h>


int main(){
    char mala[3][3]={"20","20","20"};
    int n=0,k=2,j=0,i=0,u=0;
    scanf("%d",&n);
    char caixa[3][3];
    char casos[2][6]={"good","bad"};
    char nmr[n][6];
    for(i=0;i<n;i++){
        scanf("%s %s %s",caixa[0],caixa[1],caixa[2]);
        for(j=0;j<3;j++){
            k=mala[j]-caixa[j];
            if(k<0){
                strcpy(nmr[i],casos[2]);
                break;
            }
            strcpy(nmr[i],casos[1]);
        }
    }
    for(i=0;i<n;i++){
        u=i+1;
        printf("Caso %d: %s\n",u,nmr[i]);
    }
    return 0;
}

For some reason all values enter the if, even for k>0, so any input gets "bad" as an output, can someone explain why? Thank you.

Aucun commentaire:

Enregistrer un commentaire