Why are 100 not included in this program?

I am writing this program that will guess what the user number is thinking about. After several days of work, I could not understand what was wrong with that. Also my proposed class for assignment is not what I expected. Please, help.

  • A user can guess 100, but my program uses a midpoint rule, so it can reach 99. How can I make 100 inclusive?
  • If I keep pressing "l", the program will eventually exit the loop and print. If you want to try again?
  • Is there a better way to code this program? An example, please.

Here is the real program:

Write a program in which you can find the number chosen by the person. The human user will think of a quantity from 1 to 100. The program will make guesses, and the user will tell the program to guess higher or lower. The program should find the middle of the two numbers and ask if this number is larger or lower.

#include <iostream>
using namespace std;

int main() {

    char check;
    char tryagain;

    do {
        int midpoint;
        const int MAX = 100;
        const int MIN = 1;
        int y = MAX;
        int x = MIN;

        cout << "Think of a number between 1 and 100." << endl;
        midpoint = (x + y) / 2;

        while (x != y || y != x) {
            cout << endl << "Is it" << " " << midpoint << " " << "?";
            cin >> check;

            if (check == 'l' || check == 'L') {
                y = midpoint;
                midpoint = (x + y) / 2;
            }

            else if (check == 'h' || check == 'H') {
                x = midpoint;
                midpoint = (x + y) / 2;
            }

            else if (check == 'c' || check == 'C') {
                break;
            }

            else {
                cout << "Incorrect choice." << endl;
            }
        }

        cout << "Great! Do you want to try again? (y/n)";
        cout << endl;
        cin >> tryagain;
    } while (tryagain == 'y' || tryagain != 'n');

    return 0;
}
+4
source share
3 answers

Your problem is incorrect consideration in calculations xand ysimilar to Alf suggested in the comments.

He must read

y = midpoint - 1;

and

x = midpoint + 1;

respectively. The reason is simple. You use midpointas an assumption. I think this is no longer part of the guesswork available. Your first premise is 50, x or y should be either 51 or 49 as a new low or high in the interval.

100 . , 99, "h".

x = 99 + 1;

100, midpoint

midpoint = (100 + 100) / 2; 

.

. , , .. -

+3

x y , . 100 + 99 / 2 = 99 (which is 99.5 rounded down). . :

if ( y-x < 2) midpoint = y;

+1

100, , 99. 100 ?

++ . . , , midpoint 99. midpoint = (99 + 100) / 2, 199 / 2, 99.5. 99 . - y = midpoint; y = midpoint - 1; x = midpoint; x = midpoint + 1;. . , midpoint 99, x 100, (100 + 100) / 2, 100.

"l", . ​​ ?

l, - 1. , , . , .

if (x == y) {
    // Answer was deduced
    cout << "You guessed " << x << ".\n";
}

? , .

. . , , " ". , , , . , midpoint == x, l .

0
source

Source: https://habr.com/ru/post/1670452/


All Articles