CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    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
    What compiler are you using?
    I am using Code Blocks with MinGW (GCC) compiler.

  2. #17
    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
    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");
    }
    The second example worked for me. So it needed that inclusion.

  3. #18
    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
    Which loops have you covered? There are 3 - do, for and while.
    I have covered while loops, and I have seen for loops in other examples with other languages. But I have not worked with it yet with C++ in this course.

  4. #19
    Join Date
    Sep 2016
    Posts
    22

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

    I almost have it now. But I keep going around the loop even after finding the correct number. How do I prevent this?

    Code:
    int main()
    {
        // Create an array of 100 elements
        int arr[100];
    
        // Set values to elements, ranging from 1 to 100
        for (int i = 0; i < 100; i++)
        {
            arr[i] = i + 1;
        }
    
        cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
        cin.get();
    
        int key;
        int first = arr[0];
        int last = (sizeof(arr)/sizeof(int)-1);
    
        while (first <= last)
        {
            int mid = (first + last)/2;
    
            cout << "Is it " << mid << "?" << 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 >> key;
    
            switch(key)
            {
                case 1:
                cout << "Found it!" << endl;
    
                case 2:
                last = mid +1;
                break;
    
                case 3:
                first = mid + 1;
                break;
    
                default:
                return -1;
                break;
            }
        }
    
        return 0;
    }
    Last edited by 2kaud; October 3rd, 2016 at 06:51 AM.

  5. #20
    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?

    When the response is 1 then you need to break out of the loop. So there must be some condition for the while loop to stop when the number has been found.

    Incidentally, you don't need the array as first is 1 and last is 100 at the beginning.
    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)

  6. #21
    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
    When the response is 1 then you need to break out of the loop. So there must be some condition for the while loop to stop when the number has been found.

    Incidentally, you don't need the array as first is 1 and last is 100 at the beginning.
    I need to set the first to be greater than last? That should stop it.

    But I used a return instead. This is what I have now:

    Code:
    int main()
    {
        // Create an array of 100 elements
        int arr[100];
    
        // Set values to elements, ranging from 1 to 100
        for (int i = 0; i < 100; i++)
        {
            arr[i] = i + 1;
        }
    
        cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
        cin.get();
    
        int key;
        int first = arr[0];
        int last = (sizeof(arr)/sizeof(int)-1);
    
        while (first <= last)
        {
            int mid = (first + last)/2;
    
            cout << "Is it " << mid << "?" << 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 >> key;
    
            switch(key)
            {
                case 1:
                cout << "Found it!" << endl;
                return 0;
    
                case 2:
                last = mid - 1;
                break;
    
                case 3:
                first = mid + 1;
                break;
    
                default:
                return -1;
                break;
            }
        }
    
        return 0;
    }
    I have turned it in this way. I did add a counter as the last thing before turning it in. It failed to work properly anyway. I needed to print out the number of tries the computer had made once it found the number. I did something stupid with j++ and got 1 for 3 tries (where key was 37).

    Note that I changed last = mid + 1 in case 2.

    I'm curious though, how would you do this? Why do I not need an array? Feel free to post your own solution or change mine.

  7. #22
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by cozySam View Post

    I'm curious though, how would you do this? Why do I not need an array? Feel free to post your own solution or change mine.
    You should ask yourself why you do need it. You know the upper and lower bounds are 0 and 100. Why not just set first and last directly. You don't use the array or any of the values other than the first one.

  8. #23
    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?

    Consider
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "Think of a number between 1 and 100. Press ENTER when ready." << endl;
    	cin.get();
    
    	int key;
    	int first = 1;
    	int last = 100;
    	int cnt = 0;
    
    	do {
    		int mid = (first + last) / 2;
    		++cnt;
    
    		cout << "Is it " << mid << " ?" << 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 >> key;
    
    		switch (key) {
    			case 1:
    				cout << "Found it in " << cnt << " tries" << endl;
    				break;
    
    			case 2:
    				last = mid - 1;
    				break;
    
    			case 3:
    				first = mid + 1;
    				break;
    
    			default:
    				cout << "Invalid entry." << endl;
    				--cnt;
    				break;
    		}
    
    	} while (key != 1);
    
    	return 0;
    }
    Note that this code doesn't check for incorrect responses - like always entering 2 etc. I'll leave that checking to you as an exercise!
    Last edited by 2kaud; October 3rd, 2016 at 11:38 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)

Page 2 of 2 FirstFirst 12

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