简体   繁体   中英

Why is 100 not inclusive in this program?

I am writing this program which will guess the number user is thinking about. After days of work, I could not figure out what is wrong in it. Also my proposed grade for the assignment is not what I expected. Please help.

  1. User can guess 100, but my program uses mid-point rule so can only go up to 99. How can I make 100 inclusive?
  2. If I keep pressing 'l' the program will eventually break out of loop and prints If you want to try again?
  3. Is there a better way to code this program? Example please.

Here is the actual program:

Write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower. The program should find the midpoint of the two numbers and ask if the number is higher 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;
}

Your problem is just a mis-think in the calculation of x and y like Alf suggested in the comments.

It should read

y = midpoint - 1;

and

x = midpoint + 1;

respectively. The reason is simple. You use midpoint as the guess. The guess is then no longer part of the available guesses. Your first guess is 50, x or y should then be either 51 or 49 as the new min or max in the interval.

This will also make 100 included in the available guesses. The last step in the calculation will be when midpoint was 99 and the user selects 'h'.

x = 99 + 1;

lower bound is 100, and the midpoint guess evaluates to

midpoint = (100 + 100) / 2; 

which is correct.

As for better ways to write this program. This would depend on what your course has taught you, what's in the curriculum, and so on. You might want to check out code-review

When your x and y are too close, the division of their sum produces an incorrect midpoint. 100 + 99 / 2 = 99 (which is 99.5 rounded down) . You need to check for this special case. At the end of the loop before the closing bracket insert:

if ( yx < 2) midpoint = y;

User can guess 100, but my program uses mid-point rule so can only go up to 99. How can I make 100 inclusive?

Division of integers in c++ discards any decimal. It always rounds down. Consider what happens when midpoint is 99. You get midpoint = (99 + 100) / 2 which is 199 / 2 which is 99.5 . Discarding the decimal leaves you with 99 every time. One possible solution is to change y = midpoint; to y = midpoint - 1; and x = midpoint; to x = midpoint + 1; This will prevent your application from guessing the same value more than once. With this change, when midpoint is 99, x will first be incremented to 100 giving us a new midpoint (100 + 100) / 2 which evaluates to 100 .

If I keep pressing 'l' the program will eventually break out of loop and prints If you want to try again?

If the user keeps pressing l then eventually the only possible solution is 1. It seems that you chose not to propose your guess at that point and assume the user followed the rules. Add an extra print when the answer is deduced.

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

Is there a better way to code this program? Example please.

See the first two parts of this answer. Other than that, it's difficult to say objectively what "better way to code this" means. You might want to consider a system to detect when the user is lying. For example, if midpoint == x then the user can't select l without lying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM