If statement should be true, but isn't?

This is a discussion on If statement should be true, but isn't? within the C++ Programming forums, part of the General Programming Boards category; Hi all, I seem to be having a silly issue with an IF statement that I can't figure out. I'm ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    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.
    Last edited by George88; 10-08-2011 at 06:54 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,344
    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.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    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.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,344
    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.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    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.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    >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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    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)
    instead of
    if(position_from[1] == i)

    Again, thanks. Would never have figured that out alone. I can't seem to change the title to [SOLVED], would a mod be able to? This thread may help someone in the future.

    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] - '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'";
          }
    }
    Last edited by George88; 10-09-2011 at 08:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-07-2011, 06:49 PM
  2. Intended false statement is seen as true
    By 987654 in forum C++ Programming
    Replies: 7
    Last Post: 09-17-2010, 12:00 PM
  3. Is it true that...
    By Shadow12345 in forum C# Programming
    Replies: 4
    Last Post: 09-15-2002, 11:52 PM
  4. Is this true?
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-18-2002, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21