simple??? logic equation

This should be extremely simple Boolean logic but I must be dead tired or completely stupid...

k is an ascii value that I am checking to see if it is a digit, an alphabet, an _, or a $ ( checking to see if it can be part of a valid identifier ).

I see k coming into the function as a zero but the following is evaluating to be true!

I hoping someone can see my error here as I must be missing something pretty simple and fundamental...

1
2
3
4
if ((k==36)||(k>=48 && k<=59 )||(k>=65 && k<=90)||(k==95)||(k>=97||k<=122))
  return true;
else
  return false;
Last edited on
The easy way:
return std::isalnum(k) || k == '_' || k == '$';
(also #include <cctype> )

As for your actual error, you have
(k>=97||k<=122)
when it should be
(k>=97 && k<=122).

Also, don't be afraid to use the characters themselves -- there's no need to look up the ASCII values.
That is, you can just do:
return k == '$' || k == '_' || (k >= 'A' && k <= 'Z') || /* ... */;
Last edited on
Is k an int or a char?

You could also
1
2
3
4
5
6
#include <cctype>

if ( isalnum ( k ) || char ( k ) == '_' || char ( k ) == '$' )
    return true;
else
    return false;
Last edited on
I convert the char that is in the linebuffer to an int to compare with decimal ascii values in the condition of the if statement...

so k is a 0 integer...

Thanks for catching the || instead of the &&!!!!
Topic archived. No new replies allowed.