0

I am currently working on pset3/search and when I try to compile the codes it keeps telling me that ^ error. I know that it is because I must not be returning a boolean value as needed, but I do not understand where my error is. Here is my code:

bool search(int value, int values[], int n)
{
    if(n<1)
    {
        return false;
    }else{
        for(int i=0; i<n;i++)
        {
            if(value == values[i])
            {
                return true;
                break;
            }else{
               return false;
            }
        }
    }
}

Selon moi, what I am doing is: 1. Deny 0 or negative numbers in the size 2. Comparing the value you might me lookgin for whith the ith value of the array. 2.1 If they are the same the function returns true and finishes the loop. 2.2 If not, the function returns false and continues the loop until the end of the array. After that everything ends, so in both cases you have a boolean value in return. Where is the mistake?

3
  • I don't think my question is duplicated. Thank you for the suggestion, but I didn't found that answer useful. Jun 26, 2015 at 0:46
  • what happens if the for loop is never executed? you're assuming that n is gonna be always > 0, but the compiler doesn't assume that. it's the same problem with the same solution.
    – kzidane
    Jun 26, 2015 at 0:54
  • I tried writing a return line before the for loop but then, it looks like it only runs the first if condition not the for loop. But you are right that finished the compilation error. Thanks. Jun 26, 2015 at 1:09

1 Answer 1

3

It's mostly a duplicate, but it bears repeating and could use a more specific answer here.

Your logic is right in that, assuming there are no issues with the data fed to it, the function will return a bool one way or another. The problem is that the compiler isn't smart enough to recognize the complexity of the logic.

So, the compiler looks at the structure of the code. It sees the if/else and decides that when the IF condition is false and the ELSE branch is taken, it sees a possible path where the FOR loop is not executed, so the code could reach the end without hitting a return.

So how do you fix this? It's fairly simple. Put a return statement before the closing } so that there is no possible way that the code can exit without hitting a return.

Now, having said that, the code does have some other structural issues.

First, the break will never execute. The line before is a return, so control will immediately exit the function at that point. The break can be deleted without problem.

Second, the for(int i=0; i<n;i++) will only execute the loop once because of the if/else combination. You will either execute the return true or the return false before the end of the first pass through the loop. It will never increment i. (If you added the break to avoid this, it won't work.)

Your code would be much cleaner if you did it without any ELSE clauses. (Keep the code, just get rid of the ELSE keyword.) Basically, if any of the code before the ELSE code blocks executes, it executes a return. IF it doesn't execute the return, normal processing will drop through to what is currently an ELSE clause anyways. Pseudocode:

if( n<1 )  return false;   // actually, this is redundant too.

for(int i=0; i<n;i++)   if ( found ) return true;

return false;

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • Thank you veeeeery much! ;D Jun 26, 2015 at 1:27

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