samedi 27 mai 2017

Else if statement executing unexpectedly, behaving as else statement [duplicate]

This question already has an answer here:

I'm working on a simple function to convert ascii codes to array indexes, and I'm have a bit of trouble with the else-if statement.

int getIndex(char character) {
    int code = int(character);
    int index = -1;

    if (code == int(' ')){
         index = 26;
    }  else if (int('a') <= code <= int('z')) {  
        index = code - int('a');
    }     
    return index;
}

The if condition works fine and fires on spaces, but the else if statement fires in every other case, regardless of whether the character is in the range a to z or not. Is there a problem I can't see with the condition?

Aucun commentaire:

Enregistrer un commentaire