I've just looked at every similar question in this forum and couldn't find one that helped me, so I'm posting my own. I've tried everything (except GDB because I don't quite understand find without generate for it keeps prompting for numbers - and I don't even know if that's right- and it never stops, so I just don't use it and I don't know if there's a way to run GDB with find and generate together) and nothing worked out.

Anyways, my code always returns false (unless I change the last line on my binary search to "return true" - in that case it always returns true) and I don't know if the problem is in sort or in search. From what I've been seeing on the forum, both of them seem correct, but I may be wrong.

Also, I put the "break" in the while loop because it was the way I found to make it stop being an infinite loop. And I'm not sure if I really need the last else block there either.

[Code: For academic purposes, I deleted the code after having this question answered.]


I ran your code and commented out the break statement and it ran perfectly. Why are you getting an infinite loop? Did you change something that you weren't supposed to elsewhere? Is there more to this than the posted code?

The break statement is preventing the while loop from running more than once, so unless the needle is found on the first pass, it will always drop through to the return statement that follows the while loop (which should be false, not true).

If you're still getting an infinite loop, there's something other than a break that you could use. Create an int int pass=0; before the loop. Inside the loop,

if( pass++ > 100 )
    printf("Loop terminated.\n");

The limit on pass should be greater than n to make sure it has enough passes through the loop. If the print executes, you know you still have an infinite loop running.

If this answers your question, please click on the check mark to accept. Also, please remove the code when you're done. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • After seeing your answer, I tried taking out the break statement and the code worked! I guess it was an infinite loop before I fixed my sort code and it didn't occur to me that I could try running it without it. Thank you so much! – Talita Maria Briganti Barbosa Mar 24 '16 at 1:44

You must log in to answer this question.

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