[Solved] C++ question: User think a number 1-100, program asks questions no more than 6 times to get answer. can’t get correct [closed]


As has already been pointed out in the comments section, there are at least 3 bugs in your code:

  1. Your question states that the user should think of a number between 1 and 100, but the variables min and max are initialized to 0 and 100, as if the user was supposed to think of a number between 0 and 100. Therefore, you should initialize min to 1 instead of 0.
  2. When the user replies “yes” to the question whether the number is below a certain value, you set max to this value. This does not make sense, because you know that the number cannot be this value, but must be below this value. Therefore, you should set max to this value minus 1.
  3. When min == 1 and max == 2, it would make sense for the next question your program asks to be whether the number is “< 2”, in order to determine whether the number is 1 or 2. However, in that case, your program asks whether the number is “< 1”, which does not make sense, because it already knows that the answer to that question is “no”. Therefore, instead of asking whether the number is smaller than (max + min) / 2, it would make more sense to ask whether the number is smaller than (max + min + 1) / 2.

I have cleaned up your code a bit and fixed the bugs mentioned above. Here is the code:

#include <iostream>
#include <string>

constexpr int MIN = 1;
constexpr int MAX = 100;

int main()
{
    int min{ MIN };
    int max{ MAX };
    std::string response;

    std::cout << "Think of a number between " << MIN << " and " << MAX << "." << std::endl;

    while ( min != max )
    {
        int guess = (max + min + 1) / 2;
        std::cout << "Is the number < " << guess << "? ";
        std::cin >> response;
        if ( response == "y" || response == "yes" )
        {
            max = guess - 1;
        }
        else if ( response == "n" || response == "no" )
        {
            min = guess;
        }
        else
        {
            std::cout << "Invalid response" << std::endl;
        }

        //The following line only exists for debugging purposes and can be disabled
        std::cout << "min: " << min << " max: " << max << std::endl;
    }

    std::cout << "The number is " << min << "." << std::endl;

    return 0;
}

I have rearranged the code in such a way that the numbers 1 and 100 are hard-coded only in one place, instead of several places in the program. This allows you to change the range very easily, without having to change the numbers in several places in the program.

One thing that my code does not do is stop asking after 6 questions, because it can take up to 7 questions to find the correct answer. In your question, you specified that it should ask not more than 6 times, but did not specify what should happen if it has not found the answer by then.

1

solved C++ question: User think a number 1-100, program asks questions no more than 6 times to get answer. can’t get correct [closed]