If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

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.

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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

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.