# If statement should be true, but isn't?

• 10-08-2011
George88
If statement should be true, but isn't?
Hi all,

I seem to be having a silly issue with an IF statement that I can't figure out. I'm quite new to C++ but not to code in general. I understand how logical statements work but I might be missing something here.

Just so there's no confusion. I'm trying to create a basic draughts/checkers game and the function in question is being called to move the correct piece as referrence by a grid:

1 2 3 4 5 6 7 8
a
b
c
d
e
f
g
h

The code checks the first element of the "position_from" array and looks for a letter (A-H). If it finds any of these it will then continue in to a nested loop and check for 1-8 to complete the grid referrence.

I've only just begun so only have code which checks for 'A', then continues in to a loop and checks for 1-8. My problem is, the nested IF statement I am using within the loop, has no other option but to come true, but for some reason won't.

The following code will output:
Quote:

Originally Posted by Code output
True - 'position_from[0] does equal 'A'
0 = A <- element zero contains 'A'.
1 = 1 <- element one contains '1'.

As you can see from the output, element[1] does indeed contain the number 1. With that in mind, why is the following code not working? The for loop checks 'position_from[1]' (which equals '1') against 'I', which equals 0 on the first loop then 1 on the second loop, then stops looping.

My code:
Code:

```void move_piece(char *pieces, char *position_from, char *position_to) {     if(position_from[0] == 'A')     {           cout << "True - 'position_from[0]' does equal 'A'" << endl;           for(int i = 0; i < 2; i++)           {                   if(position_from[1] == i) /* This statement must come true once */                   {                       cout << "True - 'position_from[1]' does equal 1";                   }                   else                   {                       cout << i << " = ";                       cout << position_from[i] << endl;                   }           }       }       else       {             cout << "False - 'position_from[0] does not equal 'A'";       } }```

Help is much appreciated. Thanks.
• 10-08-2011
grumpy
You are assuming, mistakenly, that the test if ('1' == 1) will be true. It will not. In the ASCII character set for example, the character '1' has a numeric value of 49.
• 10-08-2011
George88
I had a cruder form of this code earlier on today, which was working as I wanted it to.

I was testing to see the result of the loop by using a temporary "num" integer variable. I would assign the value of 'i' to num (num = i) and would then print out the result as '1' after the loop had ended, so the comparison has already shown to be working using the same format.

I don't know why it has stopped working now, though.

I can't even remember if I may have changed some minor line of code or re-wrote the if statement differently to that which I had originally.

I understand what you're saying about the ASCII code value for '1', but why would the IF statement check the ASCII value of '1' and not the actual value? I am checking the position_from[] array against an integer after all.

I hope I am making a really simple mistake here. :)
• 10-08-2011
grumpy
Comparing characters with an integer compares the numeric value.

You are saying that position_from[i] contains the value of '0'. You are then comparing that against the value of i (which, in your code) can only have the value of 0 or 1. '0' will not compare equal with either 0 or 1. That is what the test "if (position_from[1] == i)" does.
• 10-08-2011
George88
Quote:

Originally Posted by grumpy
Comparing characters with an integer compares the numeric value.

That's what I want.

Quote:

Originally Posted by grumpy
'0' will not compare equal with either 0 or 1.

Sorry, I don't understand. Would you please be able to explain why? Aren't '0' and '0' the same, which would make the comparison true? Also, this does not explain how I actually had this code working today, comparing it in the same manner.

Please excuse my newby ignorance :). Thanks.
• 10-09-2011
manasij7479
>Aren't '0' and '0' the same ...
Yes, but '0' and 0 are not the same.
'0' is..as explained above.. the numerical value of the character looking like a 0.
• 10-09-2011
George88
Thanks for your replies. The problem has been solved. The above code needs to subtract '0' from the char array during the comparison, so it becomes:

if((position_from[1] - '0') == i)
```void move_piece(char *pieces, char *position_from, char *position_to) {     if(position_from[0] == 'A')     {           cout << "True - 'position_from[0]' does equal 'A'" << endl;           for(int i = 0; i < 2; i++)           {                   if((position_from[1] - '0') == i) /* This statement must come true once */                   {                       cout << "True - 'position_from[1]' does equal 1";                   }                   else                   {                       cout << i << " = ";                       cout << position_from[i] << endl;                   }           }       }       else       {             cout << "False - 'position_from[0] does not equal 'A'";       } }```