So for an assignment I have to convert a character (0-F) to an integer (0-15), 0-9 works fine, but if any letter is given, it prints a random number: For C for instance, it gives 19, for D is returns 20.
This is my method:
int char2int(char digit) {
int i = 0;
if (digit == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9)
i = digit - '0';
else
if (digit == 'A' || 'B' || 'C' || 'D' || 'E' || 'F')
i = digit - '9';
else
i = -1;
return i;
}
At first my if statements were like this:
if (digit => 0 && =< 9)
if (digit => A && =< F)
But that gave a number of errors. You can tell I don't know C very well. My current If statement works but I'm sure it's unnecessarily long.