Number Gusser in c++ using function and midpoint

后端 未结 1 452
鱼传尺愫
鱼传尺愫 2021-01-29 13:24

I am trying to write a code for number gusser using funcitons: The playOneGame function should have a return type of void. It should implement a complete guessing game on the ra

相关标签:
1条回答
  • 2021-01-29 13:50

    The guessing loop should loop something like this:

    while (true)
    {
        int guess = getMidpoint(minimum, maximum);
        std::cout << "\nIs it [h]igher/[l]ower/[e]qual to " << guess << "? ";
        char answer;
        if (!(std::cin >> answer))
        {
            std::cerr << "error reading user input, program exiting\n";
            exit(1);
        }
        if (answer == 'h')
            minimum = guess + 1;
        else if (answer == 'l')
            maximum = guess - 1;
        else if (answer == 'e')
        {
            std::cout << "Well, isn't that nice.\n";
            return;
        }
        if (minimum > maximum)
        {
            std::cerr << "hey, you lied to me!\n";
            exit(1);
        }
    }
    
    0 讨论(0)
提交回复
热议问题