CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Number Guessing

  1. #1
    Join Date
    Feb 2013
    Location
    United Kingdom
    Posts
    2

    Number Guessing

    Hey,

    I'm going through the book "Programming: Principles and Practice Using C++" and I am currently on chapter four. I've to make a program where the users chooses a number and the computer is to guess it. One of the rules is it is to guess in no more than seven tries.

    Here is my code:

    Code:
    #include <iostream>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::string;
    
    int main(  )
    {
    	int lowerBound = 1;
    	int median = 0;
    	int upperBound = 100;
    	string answer;
    
    	// Run through loop until the difference between bounds is 2
    	while ( upperBound - lowerBound >= 2 )
    	{
    		// Calculate halfway point of range
    		median = ( lowerBound + upperBound ) / 2;
    
    		cout << "Is your number between " << lowerBound << " and " << median << "? ";
    		cin >> answer;
    
    		if ( answer == "y" || answer == "Y" )
    		{
    			// Number is within range
    			upperBound = ( lowerBound + upperBound ) / 2;
    		}
    		else
    		{
    			// Number is not within range
    			lowerBound = ( lowerBound + upperBound ) / 2;
    		}
    	}
    
    	// Now there is two numbers, it is either either of them
    	cout << "Is your number << " << lowerBound << "? ";
    	cin >> answer;
    
    	if ( answer == "y" || answer == "Y" )
    	{
    		// Number is the lower bound
    		cout << "\n\nAnswer is: " << lowerBound << "\n\n";
    	}
    	else
    	{
    		// Number is the upper bound
    		cout << "\n\nAnswer is: " << upperBound << "\n\n";
    	}
    
        return 0;
    }
    It guess with 8 or 9 tries. How could I modify it to decrease the number of tries?

    Thanks,

    Dash535

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

    Re: Number Guessing

    How could I modify it to decrease the number of tries?
    By changing the code to be correct!

    You're got several problems with the code which is why it can take more than 7 guesses. As you are trying this as an exercise, I'll first just give you some hints to see if you can work it out for yourself. You will learn more if you can figure it out for yourself rather then me just telling you the answer as the problem is logic not your use of the c++ language.

    The first problem is that if you say a number is not between a range, the starting number of the new range is the same as the ending number of the previous range - when it should be one more.

    Secondly, the while loop test is wrong. You need to be be testing that the range displayed is not the same.

    Give it another try, step through your new code with the debugger if it still doesn't do it in 7 or less and see if you can figure it out.

    Incidentially do you know why the max number of guesses is 7? (Hint it would still be 7 if the max number was 128).

    If you still can't with the hints above, come back to us with your revised code and I'll give you more hints.

    What do you think of Stroustrup's book?
    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
    Feb 2013
    Location
    United Kingdom
    Posts
    2

    Re: Number Guessing

    2kaud, thanks for your reply; It has been immensely helpful. I much prefer been pointed in the right direction, than being told the answer.

    This problem has been annoying me for a few days now, but so far I've been going round in circles.

    I've made a few changes. The most notable change is the adding of one to the lowerBound when it is changed.

    I wasn't exactly sure how to change the test for the while loop. Would I have to add two variables that contain the previous bounds and then compare the new bounds to them?

    Another change i made was to add a counter to the loop, to keep track of the number of tries.

    I've run this quite a few times, and it gets it in seven tries.

    Code:
    // Application.cpp
    
    #include <iostream>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::string;
    
    int main(  )
    {
    	int count = 0;
    	int lowerBound = 1;
    	int median = 0;
    	int upperBound = 100;
    	string answer;
    
    	// Run through loop until the difference between bounds is 2
    	while ( true )
    	{
    		// Calculate halfway point of range
    		median = ( lowerBound + upperBound ) / 2;
    
    		// Keep a track of how many tries
    		++count;
    
    		if ( lowerBound == median )
    		{
    			// Now there is two numbers, it is either either of them
    			cout << "Is your number << " << lowerBound << "? ";
    			cin >> answer;
    
    			if ( answer == "y" || answer == "Y" )
    			{
    				// Number is the lower bound
    				cout << "\n\nAnswer is: " << lowerBound << "\n";
    			}
    			else
    			{
    				// Number is the upper bound
    				cout << "\n\nAnswer is: " << upperBound << "\n";
    			}
    
    			break;
    		}
    		else
    		{
    			cout << "Is your number between " << lowerBound << " and " << median << "? ";
    			cin >> answer;
    
    			if ( answer == "y" || answer == "Y" )
    			{
    				// Number is within range
    				upperBound = median;
    			}
    			else
    			{
    				// Number is not within range
    				lowerBound = median + 1;
    			}
    		}
    	}
    
    	cout << "\nTries: " << count << "\n\n";
    
        return 0;
    }
    I'd like to modify the loop to how you previously suggested, to see how that affects things.

    As far as the book goes, it is the best book i've read yet on C++. I've bought many books and get disheartened quite quickly with them. My thought is that the Stroustrup book contains many exercises for you to do, and it is the trying to solve problems that is making things sink in.

    Thanks for your help,

    Dash535

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