Search code examples
c++for-loopif-statementwhile-looporganization

Computer guesses your number (has to be in 10 tries) from 1-1000


OK, to start off I am a complete beginner in a computer science class. I could ask my teacher, but I don't have time to do so. So, expect some really dumb errors that I can't see and I am using eclipse.

here's my code:

#include <iostream>
using namespace std;

int something()
{
    int big = 1000;//largest number is 1000
    int small = 1;//smallest number is 1

    //so, best guess is to go in the middle
    int c;//my guesses
    int inequality;//used to write if statements

    for (int a = 0; a <= 10; a++)
    {
        cout << "Think about a number between 1-1000" << endl;//what console tells you
        c = (big - small) / 2;//my guess will be the midpoint of the two numbers
        while (big > small)//while the highest number is ALWAYS greater than the lowest number
        {
            cout << "Is your number less than, equal to, or greater than my guess? 1-less,2-equal,3-greater" << c << endl;
            cin >> inequality;//you tell me whether my guess is too high, low, or equal
            if (inequality == 1)//if you say it is too low...
            {
                small = c;//the smallest number is now my last guess
                c = c - (big - small) / 2;//so, I'll take the midpoint of the CURRENT biggest and smallest number
            }
            else if (inequality == 2)//if you say it is equal...
            {
                cout << "Yay, I guessed your number." << endl;//cool.
            }
            else if (inequality == 3)//if you say it is too high...
            {
                big = c;//biggest number is now my guess
                c = c + (big - small) / 2;//so, I'll take the midpoint of the CURRENT biggest and smallest number
            }
        }
    }
    system("pause");
    return 0;//returns something in int main function
}

int main()
{
    something();//so I can actually do code.
}

So my problem:

If I enter 1 after the console enters the first guess, I get 499, which is fine. After the second guess (where I enter 1), I get 249, which is also fine. However, the third guess after I enter 1 gets a random 681, so could someone help me?

It would be most appreciated if you did not rewrite the entire code for me, otherwise that is really suspicious when I turn it in. I am struggling because I do not have very good computer science background, so to improve, I need ideas mostly. Lastly, any way to make my code look a LITTLE more professional would be appreciated :)

Also, my for loop may be a bit off, I'm not sure.


Solution

  • When you calculate next number you need to change range

    So you have first

    small     guess     big
    +---------+----------+
    

    if user says too small, then the answer is above the guess, so in the range big - guess and that is what you need to cut in half so instead of

    c = c - (big - small)/2
    
    guess = (big - guess) / 2 + guess
    

    if user says too big then the answer is between guess and small

    guess = (guess - small) / 2 + small