0

First of all I know there are two questions very similar to this one but the answer I'm looking for is not there. I'm working on my "move" function for fifteen in pset 3. I keep getting the "control reaches end of non-void function" error message when I try to compile but I don't see why. Probably my logic is off somewhere but I don't know where. Maybe I'm not accounting for a condition that could happen? Here is the gist of my code:

for(int i=0; i<d; i++)
{
    for(int j=0; j<d; j++)
    {
        //if tile exists in board
        if(board[i][j] == tile)
        {
            //if tile is next to blank space
            if(board[i-1][j] == 0)
            {
                swap stuff
                return true;
            }
            else if(board[i-1][j-1] == 0)
            {
                swap stuff
                return true;
            }
            else if(board[i][j-1] == 0)
            {
                swap stuff
                return true;
            }
            else if(board[i][j] == 0)
            {
                swap stuff
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

As far as I can tell it would return true or false in all situations. Any help appreciated.

  • Do you have a final return statement after all of this code? – Luke Van In Jul 31 '14 at 16:37
1

As you correctly pointed out, the error message "control reaches end of non-void function" means that the compiler is expecting a return statement where there is none. In other words, the compiler is indicating that you need another return statement somewhere.

While there are a series of return conditions inside the loop, it is still possible that the program can skip all these conditions and reach the end.

For example, consider what would happen in the case where d has the value of 0. The loop would not execute at all, and so none of the conditions would be evaluated, and none of the return statements would be called.

| improve this answer | |
  • How silly of me. Thanks – Sofia Jul 31 '14 at 17:26
1

Edit: I noticed Luke Van In's answer, which induced a forehead slap on my part, immediately after editing to post a correction. However, the following may still be helpful.

I see 2 problems, although I'm not 100% sure they're related to this.

The first is that, when you initialize i and j with value 0, for board[i - 1][j] and similar expressions you will be going beyond the bounds of your array, e.g.:

board[-1][0]

does not exist and may give you a segmentation fault. You'll need to check for those cases when you're at the edge of the board.

You also have a logic error when checking for a blank space. For example, if your board is laid out like so:

x x x
x O x
x x x

and O is your target, located at

board[i][j]

then in order to check the space above O, you need to look at

board[i][j + 1]

not

board[i][j]

so if there is an empty space above O, or to the right of O, you're not returning the expected value.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .