Computer's guess my number C++

Hello forum.I am truly sorry this is my first post,but as I am a terrible programmer who has just begun(or might I say,just 'properly' started) C++.I am have a problem with my 'guess my number'game where a 'computer' has to guess the numbers.But I seem to have a problem on my application.

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 number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100.
    char answer;
    int tries = 0;
    
    do {
    cout << "choose a number between 1 and 100 and I'll guess it." << endl;
    cout << "is " << number_guess << " your number?(y/n)" << endl;
    ++tries; // calculates how many tries it took the computer
    
    cin >> answer;
    
if (answer == 'y')
    cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the hardware!" <<  endl;
    
    
} while (answer == 'n'); // continue until the computer is wrong.
    
 
    
    
system("pause");
return 0;
}


Now,this is designed so that I secretly pick the number between 1 and 100,then computer has to guess it.I tell the computer if he is wrong or right(y/n) then computer either continues guessing,or he congratulates himself.

Here is the problem.

The computer,is guessing the EXACT same number each time.So what I mean by that is,actually,I'll give you an example.

lets say my computer guess 12 and he is wrong.
Next turn,he guessess 12 AGAIN.Or should I say,
HE GUESSESS THE SAME NUMBER OVER AND OVER AND OVER.

This seem to be my fault,and because I am a beginner,I can't seem to find a problem.Please help forum users.Thank you in advance.

So within that do-while loop, the computer presents the variable number_guess as its guess. I see where that value gets set, at the start; tell me, where do you ever change that value so that the computer doesn't always guess the same number?
Hmm...good point.So how should I modify my code?
Every time the do-while loops, you need to change the value of number_guess
Yes,and my question is,how could I do that?.Please explain in more detail as I am really new t this kind of things.
Insert the number_guess inside of do...while loop

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()
{
    srand(time(0)); 
    int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100.
    char answer;
    int tries = 0;
    
    do {
    cout << "choose a number between 1 and 100 and I'll guess it." << endl;
    cout << "is " << number_guess << " your number?(y/n)" << endl;
    ++tries; // calculates how many tries it took the computer
    
    cin >> answer;
    
if (answer == 'y')
    cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the hardware!" <<  endl;
    
 number_guess = rand() % 100 + 1;
    
} while (answer == 'n'); // continue until the computer is wrong.
    
 
    
    
system("pause");
return 0;
}


I suppose I moved that number_guess to the right place.

Well to explain you things, firstly you declare a variable number_guess and give him random value, later you don't change that value, but simply take the variable and check it again for equivalence. So you need after each incorrect guess add new value to variable number_guess, so just adding number_guess = rand() % 100 + 1; at the end of do...while loop is what you needed.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
    srand(time(0));
    int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100. <-- this code needs to go in the do-while loop.
    char answer;
    int tries = 0;

    do 
    {
        cout << "choose a number between 1 and 100 and I'll guess it." << endl;
        cout << "is " << number_guess << " your number?(y/n)" << endl;
        ++tries; // calculates how many tries it took the computer
        cin >> answer;
        if (answer == 'y')
        cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the hardware!" <<  endl;
    } while (answer == 'n'); // continue until the computer is wrong. <-- Do you mean until the computer is right? 
system("pause"); 
return 0;
}


This is going to take a long time for the computer to guess the number, as it is just guessing totally randomly. Perhaps you should store which numbers have already been guessed in an array?

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

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
   
    int tries = 0; 
   
    char answer;
    do {
          
    srand(time(0)); 
    int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100.
    cout << "choose a number between 1 and 100 and I'll guess it." << endl;
    cout << "is " << number_guess << " your number?(y/n)" << endl;
    ++tries; // calculates how many tries it took the computer
    
    cin >> answer;
    
if (answer == 'y')
    cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the back!" <<  endl;
    
    
 number_guess = rand() % 100 + 1; 
    
} while (answer == 'n');
 // continue until the computer is wrong.
    
 
    
    
system("pause");
return 0;
}




I took Daniels' advice and moved the guess_number outside,and I also moved the int tries outside as well,because it was generating a problem that nomatter how much tries it actually did,the computer always claimed that it did it in 1 shot.Now the problem is solved,but,,,,




This is going to take a long time for the computer to guess the number, as it is just guessing totally randomly. Perhaps you should store which numbers have already been guessed in an array?




That is a good suggestion.I would do that but I'm just not that advanced :D
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<cstdlib>
#include<ctime>
using namespace std;

int main()
{

    int tries = 0;
    srand(time(0));
    int number_guess = rand() % 100 + 1; // lets say I chose a random number between 1 and 100.
    int counter = 0;
    int array[100];
    bool errorfound = true;


    char answer;
    do {

    cout << "choose a number between 1 and 100 and I'll guess it." << endl;
    cout << "is " << number_guess << " your number?(y/n)" << endl;
    ++tries; // calculates how many tries it took the computer

    cin >> answer;

if (answer == 'y')
    cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the back!" <<  endl;
else
{
    array[counter] = number_guess;
    counter++;
}


 number_guess = rand() % 100 + 1;
 
 while(errorfound == true)
 {
 int e = 0;
     for(int i=0; i<100; i++)
     {
        if(number_guess == array[i])
        {
            number_guess = rand() % 100 + 1;
            e = 1;
            break;
        }
     }
     if(e != 1)
        errorfound = false;
     
 }

} while (answer == 'n');
 // continue until the computer is wrong.




system("pause");
return 0;
}



I suppose it might be something like that. Not compiled it and not tried, maybe with errors, but the main point is here.
Seems like using a sledge to drive in a wood nail; it's just too much.

1) Don't repeat code. If you find yourself typing exactly the same command twice, you're doing it wrong.
2) srand(time(0)); should not be used more than once per program. It's a seed generator, not a random number generator.
3) Indentation rocks. It makes people not go cross-eyed when they try to make sense of where your nests begin and end.
Personally, I'm a fan of this approach:

1
2
3
4
5
6
7
8
9
10
11
12
int random_number(){
    return 2*2;
}

int main(){
    cout << "\nThiiiink of a numba'";
    while(1){
       cout << "\nI guess : " << random_number() << "! Am I right? (y/n)\n>";
       cin.get();
    }

}


01110100011100100110111101101100011011000110011001100001011000110110010100101110011010100111000001100111
Last edited on
1
2
3
int random_number(){
    return 5; // I rolled a die for this - guaranteed random
}
This is how I would set up the array. Bool arrays such as this can be really useful. I commented the code so you can see changes I have made.
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
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
    int number_guess = 0; //Good to set variables to zero. 
    bool array[100];
    short int i = 0; //Just to go around this next while loop. 
    while(i < 100)
    {
        array[i] = 0; //Set all the array to 0.
        ++i;
    }
    srand(time(0));
    char answer;
    int tries = 0;
    cout << "choose a number between 1 and 100 and I'll guess it." << endl; //You only want to say this once, so it does not need to be in the loop.
    do
    {
        number_guess = rand() % 100 + 1;
        if(array[number_guess] == 1) //If this number in the array is true (or set to one in other words).
        {
            //Do nothing and we go around the loop again, generating a different, random number.
        }
        else //Otherwise.
        {
            array[number_guess] = 1; //Mark off that position in the array as taken.
            cout << "is " << number_guess << " your number?(y/n)" << endl; 
            ++tries; // calculates how many tries it took the computer
            cin >> answer;
            if (answer == 'y')
            cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the hardware!" <<  endl;
        }
    } while (answer == 'n'); // continue until the computer is wrong. <-- Do you mean until the computer is right?
system("pause");
return 0;
}
Thank you everyone for your answers and help.If I don't get extra help in next 2days,this thread will be resolved :D :D
What more do you need help on? Is this issue not solved?
bah,heck it,I'm just going to resolve this.Thank you guys for your help.
Topic archived. No new replies allowed.