Toesmash
Toesmash

Reputation: 327

Unrecognized variable in if statement

I have a little problem with my program. In my IF statement my variable is not being recognized.

for(i=1;i<10;i++){
    printf("%d:", i);

        for(j=0;pom[j]!='\0';j++){
                if(pom[j]==X) star++;
        }
    X++;
    printf("%d", star);
    printf("\n");
    star=0;
}

Variable X is defined as int X=0; in this function. It is supposed to be a number from 1 - 9 but its not changing its value throught the code even if I have X++; right after my for loop.

This code runs great when I remake it to if(pom[j]=='1') star++; or if(pom[j]=='2') star++; or whatever number from my desired interval, so I think the problem is in my if statement syntax. Any ideas guys? Thank you for your help.

Upvotes: 0

Views: 244

Answers (1)

Federico
Federico

Reputation: 3892

In your if statement you are comparing a char (I am supposing that pom is a char) with an int.The ASCII code of '1' is the integer numer 49, and not 1. You can do:

if (pom[j] == (x + 48))

An alternative way is to declare:

char X = '0';

Read the ASCII table

Upvotes: 3

Related Questions