Need confirmation :) - Guessing Game

Hello, so, i've got this exercise from this Beginning C++ Game Programming that i got, a Guessing game, im supposed to create a game where the user inputs a number for the computer to guess, and than the computer guessess it, when it does, it will show in how many tries he did it, i've tested this, and it seems to work just as intended, but i just feel like something isnt right, and since i have no one to ask about it personally, i was hoping one of you guys could check this code out and tell me if i did anything that i shouldnt have :)

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));
    int rNum = rand() % 100 + 1;
    int tries = 0,uNum;

    cout << " Enter the number you want PC to guess: " << endl;
    cin >> uNum;

    do
    {   srand(time(0));
        rNum = rand() % 100 +1;
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            cout << " Computer Guessed too High!" << endl;
        if(rNum < uNum)
            cout << "Computer Guessed too Low!" << endl;
    }while(rNum != uNum);

    cout << "Computer guessed your number in " << tries << " tries!" << endl;

    return 0;
}


Hope to hear from somebody soon :)
Some observations:
-Your program would only need to establish a guess once. Your loop would then determine if it needed to increase or decrease within your if blocks, in addition to the reporting that you already have.

-You seemingly ask your user for ANY number, but I think you want 100 or less.

-Is there a reason you are including cstdlib? (has nothing to do with your solution, just might be unnecessarily clogging up the namespace) EDIT: it is for the use of rand()/srand(); sorry its late, disregard!

See what you can do with the above. Would be happy to help further.
Last edited on
Make the minimum and maximum number the computer guesses variables and if the computer guesses too high, then the maximum will be lowered, otherwise the minimum will be raised. This will allow the number to actually make guesses that make sense. If 95 is too high, then the computer should never guess above 94 from that point on.

I would suggest
 
min + rand() % (max - min + 1);
for your random number line.

The rest should be pretty easy.
Last edited on
thanks for the input Cantide :) (i edited that)

GRex -

You may have already assumed that im a total n00b, but,however, you've given me something what to look for, hehe, i dont know how to do that, to set the minimum and maximum number, i tried copying your code and paste it to mine, but it ... wasnt right fitting in, i will look more into this what you mentioned,and hopefully ill get it right soon.

Latest Code

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

    int tries = 0,uNum,rNum;

    cout << " Enter the number you want PC to guess(1-100): " << endl;
    cin >> uNum;

    do
    {
        srand(time(0));
        rNum = rand() % 100 + 1;
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            cout << " Computer Guessed too High!" << endl;
        if(rNum < uNum)
            cout << "Computer Guessed too Low!" << endl;
    }while(rNum != uNum);

    cout << "Computer guessed your number in " << tries << " tries!" << endl;

    return 0;
}
Put my code in line 18 set equal to rNum, then change the min and max variables in the ifs structure, I would suggest using else ifs whenever you are using several checks for the same process so that it only has to check a max of two and a minimum of one. If rNum is greater than uNum, then max = rNum - 1, else if rNum is less than uNum, then min = rNum + 1.
something like this ?

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

    int tries = 0,uNum,rNum,max,min;

    cout << " Enter the number you want PC to guess(1-100): " << endl;
    cin >> uNum;

    do
    {
        srand(time(0));
        rNum = min + rand() % (max - min + 1);
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            cout << " Computer Guessed too High!" << endl;
            max = rNum - 1;

        if(rNum < uNum)
            cout << "Computer Guessed too Low!" << endl;
            min = rNum + 1;
    }while(rNum != uNum);

    cout << "Computer guessed your number in " << tries << " tries!" << endl;

    return 0;
}


This just generated the same random number btw, never ending.

dang ... im being hopeless ... Sorry:(
No you are not hopeless, you aren't being hard headed.

Think about that loop. As I suggested, that random number needs to be generated only as a first guess by your program, not continuously. The loop will then add or subtract accordingly.

closed account (zvRX92yv)
srand() is supposed to be used once and only once, it's for seeding.
Its getting late here and im starting to get irritated a bit from this,so i think imma take a long relaxing sleep first, and continue messing with this tomorrow again, but for now, this is what i did,

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

    int tries = 0,uNum,rNum,max,min;



    cout << " Enter the number you want PC to guess(1-100): " << endl;
    cin >> uNum;

    srand(time(0));
    rNum=rand() % 100 + 1;

    do
    {
        rNum;
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            cout << " Computer Guessed too High!" << endl;
            rNum = rNum - 1;

        if(rNum < uNum)
            cout << "Computer Guessed too Low!" << endl;
            rNum = rNum + 1;
    }while(rNum != uNum);

    cout << "Computer guessed your number in " << tries << " tries!" << endl;

    return 0;
}


i did this before the do loop,thinking, that it means the rNum gets randomly generated,but stays that way.

1
2
    srand(time(0));
    rNum=rand() % 100 + 1;


than in the do loop i call that rNum and, if rNum > than uNum i set this

rNum = rNum - 1;

thinking, it means the rNum would change from the current guess,to -1 in value , meaning it wouldnt guess that number again,it will just go -1 every time the rNum > uNum . Same for the if rNum < uNum i set it so that rNum increases to +1 ,thinking that each time the rNum is called, and if rNum < uNum it would just start increasing by +1 , not lower anymore . . . now, for tonight,i would just simply be satisfied to know, that those codes i wrote ,are really what i think they are.

I really do appreciate your help,and i hope you can bare with me for a little longer till i figure this out, it means a lot me to successfully figure this out, it would motivate me even further to learn and mess around with C++ , which i really really want to learn. Good Night for now fellas:)
Last edited on
Try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     srand(time(0));
    do
    {
        rNum = min + rand() % (max - min + 1);
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
        {
            cout << " Computer Guessed too High!" << endl;
            max = rNum - 1;
        }

        else if(rNum < uNum)
        {
            cout << "Computer Guessed too Low!" << endl;
            min = rNum + 1;
        }
    }while(rNum != uNum);
It is crashing the console, the Windows is closing it by itself . . .


PPS. My Sleep just got delayed for the next few hours or maybe not at all,cause im so happy, this code is working just perfectly i may say, a simple else if was needed for it to give me the correct response i needed!!

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

    int tries = 0,uNum,rNum;



    cout << " Enter the number you want PC to guess(1-100): " << endl;
    cin >> uNum;

    srand(time(0));
    rNum=rand() % 100 + 1;

    do
    {
        rNum = rand() % rNum + uNum;
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            {
                cout << " Computer Guessed too High!" << endl;
                rNum = rNum - 1;
            }
        else if(rNum < uNum)
            {
                cout << "Computer Guessed too Low!" << endl;
                rNum = rNum + 1;
            }


    }while(rNum != uNum);

    cout << "Computer guessed your number in " << tries << " tries!" << endl;

    return 0;
}


Mindblowing -.-' ... tried it several times now, and it seems like it generates numbers even >100 . . . Definitively going back to sleep.
Last edited on
Use my code. initialize min as 1 and max as 100. It will work.

Remove line 18.
It is generating numbers greater than 100 because you are basing your random numbers off of the previous random number and adding the user number.

If I want a number less than 20 and the user inputs 18 the program you have will give me a random number less than 20, say 12, then it will generate a random number less than 12 and add 18 to that number. Then, when it executes again, it will choose a number less than that number and add 18 to it.
I see. hmmm, anyways, this is the final code, and it seems to be working Perfectly now :) , thank you so much for guiding me through this ,much appreciated.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

    int tries = 0,uNum,rNum,max=100,min=1;



    cout << " Enter the number you want PC to guess(1-100): " << endl;
    cin >> uNum;

    srand(time(0));

    do
    {
        rNum = min + rand() % (max - min + 1);
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            {
                cout << " Computer Guessed too High!" << endl;
                max = rNum - 1;
            }
        else if(rNum < uNum)
            {
                cout << "Computer Guessed too Low!" << endl;
                min = rNum + 1;
            }


    }while(rNum != uNum);

    cout << "Computer guessed your number in " << tries << " tries!" << endl;

    return 0;
}
If you really want to make it interesting, then try this.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

    int tries = 0,uNum,rNum,max=100,min=1;



    cout << " Enter the number you want PC to guess(1-100): " << endl;
    cin >> uNum;

    srand(time(0));

    do
    {
        if(tries < 3)
             rNum = (min + max) / 2;
        else
             rNum = min + rand() % (max - min + 1);
        ++tries;
        cout << " Computer Guessed " << rNum << endl;

        if(rNum > uNum)
            {
                cout << " Computer Guessed too High!" << endl;
                max = rNum - 1;
            }
        else if(rNum < uNum)
            {
                cout << "Computer Guessed too Low!" << endl;
                min = rNum + 1;
            }


    }while(rNum != uNum);

    cout << "Computer guessed your number in " << tries << " tries!" << endl;

    return 0;
}


I haven't tried this new code, but it should allow the computer to make the first few guesses try to find the number, and then the rest to try and guess them. I think it would work a lot better in some cases.
Topic archived. No new replies allowed.