CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Sep 2016
    Posts
    22

    Post How to make a program that guesses what number I'm thinking of?

    Hello everyone!

    I am new to these forums. I come here today because I need help with a C++ programming assignment for school, and in the long term I also hope to find some mentors and friends here. I hope to revisit these forums many times to come both to get help and offer help to others as I become a proficient programmer.

    So! Here is the assignment! I need to make a C++ program that will try to guess what number I am thinking of. It's sort of like that old magic trick where the magician asks "is this your card?"

    The program will prompt the user to think of a number between 1 and 100. The program will then print out a number and ask the user to either confirm the number as the correct number (the number the user thought of), or in case the number is wrong, the user will tell the program that the number is either lower or higher than the displayed number. The program will continue guessing until the correct number is found, at which point the user will tell the program that the number is correct and the program will print out message celebrating its success at guessing the number. One important requirement is that the program may not use any more than 7 guesses to find the number.

    (By the way, do you say "program will prompt" or "computer will prompt"? Is it "user will tell the program" or "user will tell the computer"? Is it in any context important to make this kind of distinction between computer and program?)

    Here is the skeleton code I got so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
    
        cout << "Is it " << " ?" << endl;
        cout << "Press 1 and ENTER if the number is correct." << endl;
        cout << "Press 2 and ENTER if your number is lower." << endl;
        cout << "Press 3 and ENTER if your number is higher." << endl;
    
        cout << "Found it!" << endl;
    
        return 0;
    }
    I know it's not much right now. But at least I got some text on the screen. It's a start! It also outlines how the program is supposed to work. I also know how to store the user returned value and store it in a variable. But I don't know how to build the logic or the algorithm for this.

    Where do I go from here? I know those three prompts will have to iterate until the correct number is found. So do I use a while loop in here?

    The last printout will have to display at the end of the program, and only after the correct number is found. So do I use an else condition here?

    I read on the web that an algorithm called binary search is perfect for this situation. I also read that C++ has some built in functionality for this in the standard library. But my problem is that I have no idea how to implement this. I am completely green to both C++ and programming in general! I have read some tutorials, watched some tutorials, and I understand some of the fundamental concepts of programming. But I have no idea how to do something like binary search.

    The course I'm in is a beginner's course, and I don't understand how they expect us to know this kind of stuff. We haven't even learned yet how to do inclusions, library imports or how header files work and all that stuff. The course book is mediocre at best. So please Internet, can you help me? You don't have to give me a complete solution, but maybe you can give me some pointers (no pun intended) so I can finish this on my own?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    The basic idea behind a binary search is that you have a start value and an end value. The 'guess' is the 'middle' between the start and end. If the guess is the right number than great. If guess is less than the required number then the start number becomes the guess (+ 1 ?) and the process is repeated. If guess is greater then the required number then the end number becomes the guess (- 1 ?) and the process is repeated. This process loops until the guess is the required number. For a maximum number of 100, the maximum number of guesses required is indeed 7 - each time through the loop you half the available pool of numbers.

    Try coding this up and repost your code and we'll provide further guidance.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Sep 2016
    Posts
    22

    Re: How to make a program that guesses what number I'm thinking of?

    How do I make a program pause and wait for me to press a key before it continues?

    Right after the first printout I want it to pause, and then continue when I have pressed Enter. How do I do that?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    Code:
    system("pause");
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Sep 2016
    Posts
    22

    Re: How to make a program that guesses what number I'm thinking of?

    Quote Originally Posted by 2kaud View Post
    Code:
    system("pause");
    "Error! System was not declared in this scope."

    I hve put it in the main, right after the first printout line.

  6. #6
    Join Date
    Sep 2016
    Posts
    22

    Re: How to make a program that guesses what number I'm thinking of?

    Do I have to store the return value from cin before I use it in a test?

    Code:
    cin >> a;
    
    if(a<b)
    {
    	cout << "Hello!";
    }
    Can I use the returned value in a test directly?

    Code:
    if(cin>><b)
    {
    	cout << "Hello!";
    }
    I know it looks stupid and incomplete, with all those less than and greater than signs. But hey maybe I'm just using the wrong syntax, I don't know.

    P.s. No edit buton in here?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    What compiler are you using?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    This works for VS2015
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "Hello" << endl;
    	system("pause");
    }
    However, depending upon the compiler you may also need to include cstdlib
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    	cout << "Hello" << endl;
    	system("pause");
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    Quote Originally Posted by cozySam View Post
    Do I have to store the return value from cin before I use it in a test?

    Code:
    cin >> a;
    
    if(a<b)
    {
    	cout << "Hello!";
    }
    Can I use the returned value in a test directly?

    Code:
    if(cin>><b)
    {
    	cout << "Hello!";
    }
    I know it looks stupid and incomplete, with all those less than and greater than signs. But hey maybe I'm just using the wrong syntax, I don't know.

    P.s. No edit buton in here?
    Yes, you have to store the value read using cin into a variable before it can be used.
    Code:
    int num;
    cin >> num;
    This reads an integer value into the variable num.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Sep 2016
    Posts
    22

    Re: How to make a program that guesses what number I'm thinking of?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
    
        int answer, currentNumber=50;
    
        cout << "Is it " << currentNumber << "?" << endl;
        cout << "Press 1 and ENTER if the number is correct." << endl;
        cout << "Press 2 and ENTER if your number is lower." << endl;
        cout << "Press 3 and ENTER if your number is higher." << endl;
    
        cin >> answer;
    
        if(answer==1)
        {
            cout << "Found it!" << endl;
        }else
        {
            if(answer==2)
            {
                currentNumber/2;
            }else
            {
                if(answer==3)
                {
                    currentNumber;
                }
                }
            }
        }
    
        return 0;
    }
    Something like that! I don't know. I don't know if I should loop it and then do the if else, or if else and then loop it. I think I'll just give it up and shelf it until I have actually learned to program.

    I know those if else, if else look stupid, and I hate it. I also know I can use switch instead, but I am still not confident enough to use those. They are too scary!

    I think I will stick to the "hello world" for now and build from there. Maybe my teacher should teach me these things first, before giving me assignment like this one. It's a distance learning course too, and he has not answered on my last question in 2 days now (they have to answer within 4 hours of any given work day).

    I'm not looking for someone to solve this for me really. I would like more than anything to solve it on my own. But I think expectations are set too high with this assignment (it's only the second one in the course). It's just so frustrating when you have something that looks so simple and you can't solve it. I'm sure you know that feeling.

    Code:
    if(you give me some more clues and pointers)
    {
        i might give this second try
    }else
    {
        i will leave it for some later time
    }
    What's with this brace style anyway? It looks a bit stupid does it not? Having the opening brace start on a new line makes my editor confused, i.e. it doesn't always recognize what brace belongs to what other brace, especially when nesting several if and else. But this is how my teacher likes it, so I am trying to adapt to it, but I'm having hard time with it (as does my editor).
    Last edited by cozySam; September 22nd, 2016 at 03:14 AM. Reason: smiling syntax

  11. #11
    Join Date
    Sep 2016
    Posts
    22

    Re: How to make a program that guesses what number I'm thinking of?

    Can someone work through the "binary search" alg. with me here mentally? What's going on? How does it work? I know intuitively how it works, but I want to see the math in this.

    So the range is 1 to 100. You can be the user and I will be the computer (it's a flipped version of the guess the number game, i.e. flipped roles). You think of number 37. You ask me to guess the number.

    I say 50.
    You say lower.
    I take 50 and divide by 2.
    I say 25.
    You say higher.
    I do what ??? What do I do with the 25?
    I say 50 again?

    I get stuck here mentally. I know I should say 37 here (according to the teacher), but I don't see the math. How do I arrive there?

    Do I have to store and carry those previous numbers, like 50, 25, etc?

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    s = 1, e = 100
    g = (1 + 100) / 2 = 50 (integer division!)
    I say 50
    You say lower
    s = 1, e = g - 1 = 50 - 1 = 49
    g = (1 + 49) / 2 = 25
    I say 25
    You say higher
    s = g + 1 = 25 + 1 = 26, e = 49
    g = (26 + 49) / 2 = 37
    I say 37
    You say guessed it!

    The guesses etc go in a loop until the correct number is guessed.
    Last edited by 2kaud; September 22nd, 2016 at 04:18 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    Having the opening brace start on a new line makes my editor confused
    Where the braces are located is immaterial to the operation of the program and is just writing style.
    Consider
    Code:
    if (a == b) {c = d; e = f;} else {z = y; y = x;}
    This is valid but is terrible to read!

    Now consider one style
    Code:
    if (a == b) {
        c = d;
        e = f;
    } else {
        z = y;
        y = x;
    }
    Another style could be
    Code:
    if (a == b)
    {
        c = d;
        e = f;
    }
    else 
    {
        z = y;
        y = x;
    }
    or even
    Code:
    if (a ==b)
    {
        c = d;
        e = f;
    } else 
    {
        z = y;
        y = x;
    }
    A lot of style is personal preference (or in-house coding style if working for a company) for code readability.

    Note that if there is only one statement in the then or else part then braces are not needed. Consider
    Code:
    if (a == b)
       c = d;
    else
       e = f;
    Last edited by 2kaud; September 22nd, 2016 at 04:32 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    I don't know if I should loop it and then do the if else, or if else and then loop it
    Which loops have you covered? There are 3 - do, for and while.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to make a program that guesses what number I'm thinking of?

    Maybe my teacher should teach me these things first, before giving me assignment like this one. It's a distance learning course too,
    You might find this site useful
    http://www.learncpp.com/

    What book(s) are you using from which to study c++?

    I also know I can use switch instead, but I am still not confident enough to use those. They are too scary!
    If you that that's scary, wait until you start to cover some of the advanced c++ stuff like template meta-programming!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured