mercredi 18 juillet 2018

What is a more elegant way of dealing with multiple or's in an if statment

I have this code to check every char in a char array if it satisfies a set number of properties: * Is a number * or is a (+, -, *, /)

bool chkArray(char input[]) {
for (auto x = 0; x < strlen(input); x++) {
    if (isdigit(input[x]) || input[x] == '+' || input[x] == '-' || input[x] == '*' || input[x] == '/' || input[x] == ' ') {
        continue;
    }
    else {
        return false;
    }
}
return true;

}

I feel like there is a more elegant way of dealing with the multiple or's that check for the (+, -, *, /). Something like this:

bool chkArray(char input[]) {
for (auto x = 0; x < strlen(input); x++) {
    if (isdigit(input[x]) || input[x] == '+', '-', '*', '/', ' ') {
        continue;
    }
    else {
        return false;
    }
}
return true;

}

So I am now wondering if anybody has an alternative to the original code to make it more elegant ?

Aucun commentaire:

Enregistrer un commentaire