Binary Search Help Repost.

This is a discussion on Binary Search Help Repost. within the C++ Programming forums, part of the General Programming Boards category; I've got the logic right and the algo seems to work fine. Now, that's epic for me
But..
I want ...

Binary Search Help Repost.

I've got the logic right and the algo seems to work fine. Now, that's epic for me
But..

I want to add 2 more features to this code:

1) Exit on pressing 'x'. For now I'm using -1 to exit.2) Print a message "Element not found" when an element not present in the array is searched for. Yeah, I'm not able to figure that out :|Well, Here goes my code:

You should read the input into a string then. If the input matches "x", exit, otherwise parse the string into an int. (A stringstream can help.)

Originally Posted by Sid_TheBeginner

2) Print a message "Element not found" when an element not present in the array is searched for.

Start by changing BinSearch to account for the possibility that the searchKey is not in data. Actually, it looks like you already test for searchKey == -1 in main, so you could just change the resulting message, but BinSearch does not actually return -1.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Thanks for your reply! I'm now returning -1 outside the for loop in the BinSearch function but I'm not able to figure out how to print a message using that return value -1 And stringstream hasn't been introduced yet in the book I'm following. At this point, I'm familiar with loops, switch statement, functions and arrays.

Like you wrote an if statement for binary search, you will need one for the return value of binary search if you want to do anything after a failed search.

As for ending the program with x: currently you ask the user for integers which are searchKeys. One idea is to keep asking for searchKeys, but eventually that will stop working because x is not a number. So you will have to check the stream state every time. Then you have to clear() the stream when it goes bad and read your x. If your book doesn't explain how to check the stream state or the clear() function to you, learn those anyway. It's actually the only other way I can think of that doesn't involve a lot of crazy work, and for what it's worth, the knowledge is good for you anyway.

I tried to explain the x thing. If your book really wants you to do that it should have talked about testing the state of cin somewhere. I've gone through a couple books myself and they all talk about validating inputs. That's what you need to do to do this. Think about it.

When the user stops typing integers, the idea is that the read for the next integer will fail and put the stream into a bad state. You need to clear() the stream and read the x that you are expecting. Check what you read and if it is x, end the program. I hope that is helpful.