0

I red all the questions with the code about the same issue but I can't figure out what is wrong with my code. When I use the command ./generate 1000 50 | ./find 127 I get: Didn't find needle in haystack. What am I doing wrong?

bool search(int value, int values[], int n)

{

if (n < 1)
{

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

}

When I write make find in the terminator I get the next error:

helpers.c:29:5: error: expected expression

else

^

What am I doing wrong?

2 Answers 2

1

Hint only: if (n < 1) is the problem. Review the arguments to search.

2
  • I changed the if statement. The problem might be in something else. When I write make find. I get the error: expected expression. else ^, Wait I will edit my question.. Then it's more clear.
    – Kiwi
    Commented Nov 6, 2015 at 16:03
  • Did you perhaps forget to "remove" the original return false (and closing brace) that came with the distro code? Commented Nov 6, 2015 at 16:35
1

New answer, based on new information. There's something wrong with your parentheses. That "else" has no "if" preceding it. It isn't with the outer if because it is inside that code block. It isn't with the inner if because the curly brace above the else closes the for block. Hopefully color coded braces will help.

matching braces

2
  • I placed the else statement now after the first if statement ends and the next error pups up: control may reach end of non-void function...
    – Kiwi
    Commented Nov 6, 2015 at 17:27
  • Hmmm, indeed it would. You want your for loop to process everything in values. The compiler is giving a great hint. That's not what will happen. Go through it on paper. Use a small example, like search for 2 in {1,2,3}. Will your program find it? The compiler and I don't think so. Commented Nov 6, 2015 at 17:50

You must log in to answer this question.

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