dimanche 24 novembre 2019

Comparing two strings but not getting inside the if statements

I am trying to compare the mat array with the strings in order to get a sum but it won't pass the if statement with strcmp, even though I tested the comparison and the result is 0 (the test was done on paper). I read that it has something to do with the number of array cells but I played around with it and got nowhere. I tried to use a different array for it didn't find any luck. Any help is appreciated. (The program is bigger but I am facing problems here.)

int main(void)
{
    char ans[5] = "", pay[5], mat[128];
    char more[5] = "add";
    int retpa = 0, retg = 0, reta = 0, retpl = 0, retc = 0;
    int pl = 0, qu, d, codesum, i, comppa;
    long long int code;
    float ret, sum;
    char paper[] = "paper";

    printf("letsa go\n");
    for(; strcmp(ans, "ok"); scanf("%s", ans))
    {
        for(;strcmp(more,"end"); scanf("%s", more))
        {
            printf("[paper|glass|aluminium|plastic|cloth]:\n"); 
            scanf("%s", mat);

            while(1)
            {   
                printf("[1...10]:\n");
                scanf("%d", &qu);
                if(qu < 1 || qu > 10)
                {
                    printf("again\n");
                    scanf("%d", &qu);
                }
                else
                    break;
            }

            printf("Both material and quantity %s %d\n", mat, qu);
            comppa = (mat == paper);

            if(!comppa) 
            {
                printf("paper\n");
                retpa += qu * 0.10;
            }
            if(strcmp(mat, "glass") == 0)   
            {
                printf("glass\n");
                retg += qu * 0.20;
            }
            if(strcmp(mat, "aluminium") == 0)
            {
                printf("alumin\n");
                reta += qu * 0.15;
            }
            if(strcmp(mat, "plastic") == 0)
            {
                printf("plastic\n");
                retpl += qu * 0.05;
            }
            if(strcmp(mat, "cloth") == 0)
            {
                printf("cloth\n");
                retc += qu * 0.50;
            }

            printf("%d %d %d %d %d\n", retpa, retg, reta, retpl, retc);
            printf("[add|end]:\n");
        }

Aucun commentaire:

Enregistrer un commentaire