Need Help!!

This is a discussion on Need Help!! within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a game where a person thinks of a digit between 1 and 100 and computer ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Need Help!!

    I am trying to write a game where a person thinks of a digit between 1 and 100 and computer has to guess it by asking you questions which you can only repond by typing '>', '<', or '='. What I wrote so far works only if the digit you think is 100 or 99. I can continue adding more lines in the program so that it covers all digits between 1 and 100, but seems like there should be a more efficient way of accomplishing this problem. If anyone knows of a better and faster way of doing it, please HELP. I will deeply appreciate that. Thanks a lot.
    Arooj. Here is what I wrote:


    #include <iostream.h>
    #include <conio.h>

    void main()
    {
    clrscr();
    char ans;
    cout << "Think of a number between 1 and 100 and I will\n"
    "tell you the number that you thought.\n";
    cout << "\nStrike any key when you are ready . . . . .";
    getch();
    clrscr();
    cout << "\n\nOk, I am going to ask you a few questions and I just want you to\nrespond by entering '>' '<' or '=' as your answer.\n\n";
    cout << "Is this number greater than, equal to, or or less than "
    << 50? ";
    cin >> ans;
    if (ans == '>')
    {
    cout << "\nIs the number greater than, equal to, or less than " << (51 + 100 / 2) << ": " ;
    cin >> ans;
    if (ans == '>'){
    cout << "\nIs the number greater than, equal to, or less than " << (76 + 100) / 2 << ": " ;
    cin >> ans;
    if (ans == '>')
    cout << "\nIs the number greater than, equal to, or less than " << (88 + 100) / 2 << ": " ;
    cin >> ans;
    if (ans == '>')
    cout << "\nIs the number greater than, equal to, or less than " << (94 + 100) / 2 << ": " ;
    cin >> ans;
    if (ans == '>')
    cout << "\nIs the number greater than, equal to, or less than " << (97 + 100) / 2 << ": " ;
    cin >> ans;
    if (ans == '>')
    cout << "\nIs the number greater than, equal to, or less than " << (98 + 100) / 2 << ": " ;
    cin >> ans;
    if (ans == '>')
    cout << "The number is 100. ";
    else
    cout << "The number is 99. ";

    }
    }


    getch();
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    You have the right train of though. Just think out the binary search a bit more. Use a loop to make it more efficient, so to speak.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The easiest thing for you to do is what is know as a binary search.

    range 0-100. midpoint 50

    is number < 50
    if yes try 25
    if no try 75
    is number < 25 | 75
    if yes try 13|67
    if no try 37|87

    etc.
    eventually u will find number in a smallish number of comparisons.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    not too long ago i tried to do that exact same thing
    help with making a basic game
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    15
    Thanks all very very much. I followed the advice and came up with a better code than what I had written before.
    #include <iostream.h>
    #include <conio.h>

    void main()
    {
    clrscr();
    int low = 0, high = 100, mid = (low + high)/2;
    char ans = '0';

    cout << "Think of a number between 1 and 100 and I will\n"
    "tell you the number that you thought.\n";
    cout << "\nStrike any key when you are ready . . . . .";
    getch();
    clrscr();
    cout << "\n\nOk, I am going to ask you a few questions and I just "
    "want you to\nrespond by entering '>' '<' or '=' as your answer.\n\nIf"
    " by mistake you enter incorrect answer, type = to end the game\n"
    "and start all over again.\n\n";

    while ( ans != '=') {
    cout << "\nIs it greater than, equal to or less than: " << mid << endl;
    cin >> ans;
    if (ans == '>')
    low = mid +1;
    else
    if (ans != '=')
    high = mid;
    mid = (low + high)/2;
    }
    cout << "\nThe answer is: " << mid;

    getch();

    }
    arj

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21