how can I use less code here? (simple if statement)

i'm trying to compare a strings character with several characters and if it is not equal to any of them then throw an exception. The code I tried using was:

if (hexString[i] != 'A' || 'B' || 'C' || 'D' || 'E' || 'F') (didnt work of course)

i'm trying to avoid having to do:
if(hexString[i] != 'A' && hexString[i] != 'B' && etc...
Last edited on
if (hexString[i] != 'A' || 'B' || 'C' || 'D' || 'E' || 'F') won't work (if hexString[i] != A is true, or B is true, or C is true...)

if(hexString[i] != 'A' && hexString[i] != 'B' && etc... will work (if hexString[i] != A is true and hexString[i] != B is true...)

I don't know of a way of typing it easier, but your username just made my day.
Last edited on
1
2
3
4
if(hexString[i] < 'A' || hexString[i] > 'F')
{
  // error
}
Last edited on
A portable way:

1
2
3
4
5
6
#include <cctype>

if ( std::isxdigit(hexString[i]) )
{
     // ...
}
Topic archived. No new replies allowed.