Binary search guess game in C++ I'm trying to write a program where computer attempts to guess a number entered by user. Computer should keep asking until guessed number will be equal to user number.

If you know what can be improved to make this code more readable, if implementation of the binary search algorithm is efficient, let me know!

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

int main() {
    std::srand(time(nullptr));
    constexpr int MIN = 1;
    constexpr int MAX = 100;
    std::cout &lt;&lt; "Choose a number from " &lt;&lt; MIN &lt;&lt; " to " &lt;&lt; MAX &lt;&lt; "\n";
    int number;
    std::cin &gt;&gt; number;
    int high = MAX;
    int low = MIN;
    int guess = (high + low) / 2;
    char lessThan = std::rand() % 2 == 0 ? 'y' : 'n';
    char greaterThan = std::rand() % 2 == 0 ? 'y' : 'n';
    while (greaterThan == lessThan)
        greaterThan = std::rand() % 2 == 0 ? 'y' : 'n';
    while (guess != number) {
        if (lessThan == 'y' &amp;&amp; greaterThan == 'n') {
            std::cout &lt;&lt; "Is your number less than " &lt;&lt; guess &lt;&lt; "? [y|n]\n";
            std::cin &gt;&gt; lessThan;
            if (lessThan == 'y') {
                high = guess - 1;
                greaterThan = 'n';
            } else if (lessThan == 'n') {
                low = guess + 1;
                greaterThan = 'y';
            }
        } else if (greaterThan == 'y' &amp;&amp; lessThan == 'n') {
            std::cout &lt;&lt; "Is your number greater than " &lt;&lt; guess &lt;&lt; "? [y|n]\n";
            std::cin &gt;&gt; greaterThan;
            if (greaterThan == 'y') {
                low = guess + 1;
                lessThan = 'n';
            } else if (greaterThan == 'n') {
                high = guess - 1;
                lessThan = 'y';
            }
        }
        guess = (high + low) / 2;
    }
    std::cout &lt;&lt; "I guessed!\n";
    return 0;
}
I don't know, if you can understand all my code, but I would implement the task this way:

#include &lt;iostream&gt;
#include &lt;random&gt;
#include &lt;string&gt;

enum class CompareAnswer { LESS, LESS_OR_EQUAL, GREATER, GREATER_OR_EQUAL };
enum class TruthAnswer { YES, NO };

CompareAnswer ask_user_compare(int compare_with) {
  static auto randfn = []() -&gt; bool {
    static auto dist = std::uniform_int_distribution&lt;&gt;(0, 1);
    static auto gen = std::default_random_engine();
    return static_cast&lt;bool&gt;( dist(gen) );
  };

  char user_input;
  std::cout &lt;&lt; "Is your number ";

  if (randfn()) {
    std::cout &lt;&lt; "less than " &lt;&lt; compare_with &lt;&lt; "? [y|n]\n";
    std::cin &gt;&gt; user_input;
    if (user_input == 'y')
      return CompareAnswer::LESS;
    if (user_input == 'n')
      return CompareAnswer::GREATER_OR_EQUAL;
    throw std::runtime_error("invalid answer");
  };

  std::cout &lt;&lt; "greater than " &lt;&lt; compare_with &lt;&lt; "? [y|n]\n";
  std::cin &gt;&gt; user_input;
  if (user_input == 'y')
    return CompareAnswer::GREATER;
  if (user_input == 'n')
    return CompareAnswer::LESS_OR_EQUAL;
  throw std::runtime_error("invalid answer");
};

TruthAnswer ask_user_is(int number) {
  std::cout &lt;&lt; "Is your number " &lt;&lt; number &lt;&lt; "? [y|n]\n";
  char user_input;
  std::cin &gt;&gt; user_input;

  if (user_input == 'y')
    return TruthAnswer::YES;
  if (user_input == 'n')
    return TruthAnswer::NO;
  throw std::runtime_error("invalid answer");
}

int guess_number(const int min, const int max) {
  // std::cout &lt;&lt; "\ndebug: min=" &lt;&lt; min &lt;&lt; " max=" &lt;&lt; max &lt;&lt; "\n";

  const int median = (min + max) / 2;  // (77+78)/2  =77

  if (median == min) {
    return ask_user_is(min) == TruthAnswer::YES ? min : max;
  };

  switch (ask_user_compare(median)) {
    case CompareAnswer::GREATER:
      return guess_number(median + 1, max);
    case CompareAnswer::GREATER_OR_EQUAL:
      return guess_number(median, max);
    case CompareAnswer::LESS:
      return guess_number(min, median - 1);
    case CompareAnswer::LESS_OR_EQUAL:
      return guess_number(min, median);
    default:
      throw std::logic_error("need case for some of 'enum Answer' ");
  };
};

int main() {
  constexpr int MIN = 0;
  constexpr int MAX = 10000;

  std::cout &lt;&lt; "Choose a number from " &lt;&lt; MIN &lt;&lt; " to " &lt;&lt; MAX &lt;&lt; "\n";
  int number;
  std::cin &gt;&gt; number;
  if(number &lt; MIN || number &gt; MAX) throw std::runtime_error("Invalid number");

  int guessed = guess_number(MIN, MAX);
  std::cout &lt;&lt; "Your number is: " &lt;&lt; guessed &lt;&lt; "\n";
}


Even though guess_number is recursive function, probability of stack overflow extremely low (cause log(N) complexity). Asking user again instead of throwing exception may be good idea too. 

Also:
Generally forget about srand, use &lt;random&gt; instead (srand is only for homework)