Breaking out of an infinite loop

Started by
12 comments, last by Brother Bob 11 years, 10 months ago
something like:

for(;;)
{
std::string input;
std::cout << "Enter value(q to quit): ";
std::cin >> input;
if(input[0] == 'q')
break;
int value = std::atoi(input.c_str());
//...
}
Advertisement

The issue is that you are storing a 'q' into an integer, I'm assuming it tries to give the numeric value of character q....

No, if the input cannot be parsed as an integer the extraction operator will just set the fail bit on the stream to signal a failed parse.
I've given up this program for now. I curse my speed. I'm just rushing wacko.png I'm gonna come back to it tomorrow and try to reconstruct the whole program. On some other forum I'm told that this is not even binary search huh.png Thanks a lot for the help. rolleyes.gif
I wasn't even looking at the search algorithm itself since the question was focusing on how to parse the input. But that's correct, it's not a proper binary search.

You can easily see that from the output (well, if you look for it in the first place :) ), because you have 23 elements in the array so a binary search will never look at more than 5 elements before finding the element or concluding that it doesn't exist. If you have 6 or more array lookups to compare with the value, there is something wrong with the search method.

This topic is closed to new replies.

Advertisement