number guessing issues

Hello.
I am fairly new to programming and I'm stuck with an assignment. I know there are many already done programms like this, but since it's for University I wanted to give it my own try.

We had to make a program that would guess a number between 1-100, doesn't matter how many tries. Idea is you had to tell the program if your number is higher or lower than it's guess. Well mine works for the first 3 rounds, then it goes completely bonkers. So yeah, I pretty much need somebody to tell me where/what I managed to mess up. Thanks. :)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
	{


	int vermutung = rand() % 100 + 1;  
	int antwort;
    int min = 1, max = 100;
	char x;
	bool guess;

    cout << "Please think of a number between 1-100 and press R when you're ready." << endl;
    cin >> x;


    do
	{
	    cout<<"My guess is: "<< vermutung << endl;
	    cout<<"The number is: 1(too low), 2(too high), 3(right)?" << endl;
	    cin>> antwort;
	    guess = false;

	    if (antwort == 1)                    //If answer is too low
	    {
                min=vermutung;
                vermutung=rand() % (max-min)+min;
        }

        else if (antwort == 2)           //If answer is too high
	    {
	        max=vermutung;
	        vermutung=rand() % (max+min);
        }

	    else if (antwort == 3)
	    {
	        cout<<"I guessed the number!";
	        guess = true;
        }
        }while(!guess);

return 0;

}

I would put a continue statement after each set of if statements, that way you don't check each one after you have found the correct one.

i think this:
 
vermutung=rand() % (max-min)+min;


should be:
 
vermutung=rand() % (max-min)+1;

Several comments. You use different algorithms on lines 30 and 36 for the next guess. That doesn't make much sense. The next guess depends on the values of max and min, but does not depend on what was the previous guess. Therefore the algorithm should be the same in both cases.

As a user, it is irritating when the computer makes an identical guess several times. That can be fixed by setting max or min to one less or one more than the last guess.

During testing, it's ok to have the same sequence of random numbers each time. But when the program is mostly debugged, you should seed the random number generator with srand(time(0)); at the start.

Also I saw the computer guess numbers less than 1 and more than 100, so the algorithm needs double-checking to ensure it is always within the required range.
Also , the computer guess is made on three separate lines.
It could be done just once, immediately inside the do-while loop.

The algorithm should use the values of min and max, to give precisely the same effect when these are 1 and 100 as the existing code on line 10. Line 10 need only define the variable, it doesn't need a value there.

Note: I tend to look for ways of reducing repetitive code, which is the basis of my thinking here. My reasons for doing so here is that if the computer guess is in some way strange, there will be just one place to look in order to fix it, rather than the current situation where it could be one of several places.

Edit: Actually my suggestion changes the logic, in the case where the user fails to input 1, 2 or 3. So maybe I'm not entirely correct here. This could be dealt with via another bool variable, but whether it's worth the trouble I'm not sure.

Last edited on
Hey Doubt,

It looks like the reason for your troubles is in the rand function on line 36. There is no reason for any of your calls to the rand function to be different.

In line 30, assuming the guessed number was 34 and was too low you then have the

rand() % (100-34)+34;
This will generate a number within 0 and 65 plus 34. That means you'll have a number between 34 and 99.

Then on line 36 you have

rand() % (max+min);
Assuming that we had the result above and the second number guessed was 89 which was to high you end up with a number between 0 and 88 + 34. This gives you a number between 34 and 122.

Hopefully from this you can see why the results may be completely bonkers haha.

It might be helpful to write the function as follows:

1
2
3
4
5
6
int numberGenerator(int max, int min)
{
     int result;
     result = rand() % (max-min)+min+1;
     return result;
}


Using this function in place of the function called on line 34, you would have max set at 89 and min set at 34. This would give you a number in the range of 0 and 54 + 34 + 1. Which would turn out being between 35 and 89.

Note that you'll have to have min == 0 at the start of the program. You can see that it would work for the initial random function as well:

rand() % 100

Gives you a number between 0 and 99, + 1 gives a range between 1 and 100.

So if you subtract 0 from 100 you get a range of 0 and 99, add a 0 and you get 0 and 99, add the additional 1 and you get a number between 1 and 100. This way you can use the same function for every call and just keep passing the changing min and max variables to it.
Topic archived. No new replies allowed.