Thread: Curious Problem using Logical Or operator

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    7

    Curious Problem using Logical Or operator

    Ok. I've run this through gdb (with my rather limited knowledge of it), and have tried going through and looking at all the parts of the code that I could think of that might be causing the problem.

    What the code is supposed to be:

    choose5.c is a 'guesser' for a game of battleship. It gives the next guess, and is fed the Status of the last guess (which are in an enum in the battleship.h).

    testRun3.c is a way for me to test how choose5.c is working. If choose5.c chooses a space with a ship on it, that ships counter is incremented until it reaches the size of the ship, and the Status is returned as SUNK(Ship name).
    This is where (one) of the problems lies. Battleship is getting incremented when *nextRow is 8 and *nextColumn 0.

    The weird thing is, Battleship only takes up Row 8, Columns 4,5,6,7



    testRun3.c
    Code:
                            if(*nextColumn == 0)
                            {
                               if(*nextRow == 7 || 8 || 9)
                               {
                                  Submarine++;
                                  if(Submarine == 3)
                                  {
                                     result = SUNKSubmarine;
                                  }
                               }
                            }
                            if(*nextRow == 5)
                            {
                               if(*nextColumn == 4 || 5 || 6)
                               {
                                  Frigate++;
                                  if(Frigate == 3)
                                  {
                                     result = SUNKFrigate;
                                  }
                               }
                            }
                            if(*nextRow == 8)
                            {
                               if(*nextColumn == 4 || 5 || 6 || 7)
                               {
                                  Battleship++;
                                  if(Battleship == 4)
                                  {
                                     result = SUNKBattleship;
                                  }
                               }
                            }


    So at the same time, both Submarine row[7,8,9] column[0] and Battleship are incremented.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Logical operators don't work the way you're thinking. Not:
    Code:
    if(*nextRow == 7 || 8 || 9)
    
    
    but
    if(*nextRow == 7 || *nextRow ==  8 || *nextRow == 9)

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Ok, thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. logical problem in c/c++
    By Rashid Ansari in forum C++ Programming
    Replies: 2
    Last Post: 09-10-2005, 01:56 AM
  3. Operator Overload problem
    By Kasatka in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2004, 09:29 PM
  4. overload insert operator problem
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 04:34 PM
  5. problem with new operator
    By codefx in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2002, 05:04 PM