Need help with "rand" values in a guessing game.

Im trying to get a guessing game to work.

I want the user to think of a number between 1 and 100, and let the computer guess the number in 7 tries or less.

after the computer guess'd the user get 3 choice's

1 the number is right!
2 the number is lower!
3 the number is hight!

I cant get the variable in each IF: to save the value of the random number,
so it wont guess higher or lower if the user pick one of the choice's.



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
49
50
51
52
include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    srand(time(NULL));

    int tries = 0;
    int answer;
    int low;
    int high;
    int guess = rand()% 100 +1;

    cout << " _____________________________________________________________" << endl;
    cout << "|Think of a number between 1 and 100.                         |" << endl;
    cout << "|_____________________________________________________________|" << endl;

    do
    {
        cin.get();
        cout << endl;
        cout << "I guess: " << guess << endl;
        cout << endl;
        cout << "Press 1 it the number is right." << endl;
        cout << "Press 2 if the number is lower." << endl;
        cout << "Press 3 if the nummber is higher." << endl;
        cout << "And press ENTER." << endl;
        cin >> answer;
        tries++;

        if(answer == 2)
        {
            guess = guess % guess;
            
        }

        else if(answer == 3)
        {
            
        }


    }
    while(answer != 1);

        cout << "Hurray you guess right in " << tries << " tries!" << endl;


    return 0;
}
Maybe you should try initializing and then updating the variables low and high and then use them when you're formulating the guess.
Oh, I forgot that I had them there. I tried to get it to work with them before.
But I only seem to get the lower function to work, not the other way.

This is working..
1
2
3
4
5
6
if(answer == 2)
        {
            low = rand() % guess +1;
            guess = low;

        }


But this ain't..
1
2
3
4
5
else if(answer == 3)
        {
            high = guess;
            guess = rand(); guess < high;
        }
Line 35: What do you think the result will be? Hint: any number modulo itself will be zero.

edit: The second statement on line 4 does not do anything:
 
guess < high;


Last edited on
guess = guess % guess;

I don't understand why you have this, as it will just set guess to zero, as guess / guess will not have a remainder. Why not just use guess = 0; If that is what you are after.
yeah line 35 is crap ^^

Changed it for:

low = rand() % guess +1;
guess = low;


About: guess < high;

I dont know how to raise the value of guess, thats my problem.
Last edited on
The general formula for generating a random number between low and high is
rand() % (high - low + 1) + low.

So all you need to do is make sure to update low and/or high each time and the guesses will always be in the correct range.
Initially, the number may be anywhere within the range between 1 and 100, so
 
    guess = rand()% 100 +1;
is correct. However, you should think of that in terms of the variables low and high, where low=1 and high=100.

How can you rewrite this line guess = rand()% 100 +1 so that neither 1 nor 100 is used, instead substitute something which uses the variables low and high.
Last edited on
You should probably make the computer guess halfway between current valid range every time as this will remove half the possible numbers every try.
Last edited on
How do i make the computer do that CodeGoogles?

And I have one more problem, the computer guessing negative number, I dont want that.
Last edited on
the computer guessing negative number
rand() will always generate a positive integer. What is the actual code you use to produce the negative number?

Please see also this post: http://www.cplusplus.com/forum/general/134844/#msg720077

Or as CodeGoggles suggested, "halfway between current valid range". How do you do that? Simple arithmetic, take the average of the start and end values.
Last edited on
This is how it looks atm, all messed up! need to sort it up.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    srand(time(NULL));

    int tries = 0;
    int answer;
    int low = 1;
    int high = 100;
    int guess = rand() % (high - low + 1) + low;

    cout << " _____________________________________________________________" << endl; 
    cout << "|Think of a number between 1 and 100.|" << endl;
    cout << "|_____________________________________________________________|" << endl;

    do
    {
        cin.get();
        cout << endl;
        cout << "I guess : " << guess << endl; 
        cout << endl;
        cout << "Press 1 If its right." << endl; 
        cout << "Press 2 If its lower." << endl;
        cout << "Press 3 if its higher." << endl;
        cout << "End with ENTER." << endl;
        cin >> answer;
        tries++; 

        if(answer == 2)
        {

            //low = rand() % guess +1;
            //guess = low;
            high = guess + 1;
            guess = rand() % (high - low) + 1;
            //guess = rand() % high + low;
            //guess = rand() % (high + low + 1) - low;


        }

        else if(answer == 3)
        {
            int high;
            low = guess;
            guess = rand() % (high - low + 1) + low;
        }


    }
    while(answer != 1);

        cout << "Hurray! I guess'd right in " << tries << endl;





    cin.get();
    return 0;
}
There may be other issues. However at line 48 is this:
 
    int high;


There are at least two problems with that. First, the local variable will hide the previous declaration of high at line 13. Second it is not initialised, so that its value will be whatever garbage happens to remain in that particular memory location from some previous use. And third, it doesn't serve any purpose, other than, as you said, to make it "all messed up!"

Edit:
You might also find it useful for diagnostic purposes to add this line:
 
        cout << "low: " << low << "    high: " << high << endl;

just inside the start of the do loop, insert it after line 21.

Then you can keep track of the current range as the program progresses. (of course remove it when it's all working correctly).
Last edited on
Got it to work now! thx for the help everybody!
the diagnostic tip made alot!
Last edited on
Topic archived. No new replies allowed.