Please Help Computer Guessing Game

Hi im trying to get the computer to guess my number but it wont work, what is wrong, how can i fix it?

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

using namespace std;

int one, two=90, tries;
string user_input;
bool guessed = true;
int main()
{
   srand(time(0));
   cout << "USER - Im thinking of a number between 1 and 100" << endl;
   one = rand() % two;
   do {
   one = rand() % two;
   cout << "COMPUTER - Is you number " << one << endl;
   cin >> user_input;
   if(user_input == "higher") {
    two = one + 1;
    ++tries;
   }
   if(user_input == "lower") {
    two = one - 1;
    ++tries;
   }
   if(user_input == "yes") {
    ++tries;
    break;
   }
   } while(guessed = true);
    cout << "Good Game, It only took me " << tries << " attempt(s) to guess your number";
    return 0;
}
Deleted previous reply, but you need to make guessed=false at some point.
Last edited on
It is a real idiot but it works :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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int top, down, tries, num;
string user_input;
bool guessed = true;
int main()
{
   srand(time(NULL));

   cout << "USER - Im thinking of a number between 1 and 100" << endl;

   tries = 1;
   top = 100;
   down = 1;


   while( 1 )
   {

        num = (rand() % (top-down) + down);
        cout << "COMPUTER - Is your number " << num << endl;
        cin >> user_input;

        if(user_input == "higher")
        {
            down = num;
            ++tries;
        }

        if(user_input == "lower")
        {
            top = num;
            ++tries;
        }

        if(user_input == "yes")
        {
            ++tries;
            break;
        }
   }
    cout << "Good Game, It only took me " << tries << " attempt(s) to guess your number";
    return 0;
}
Last edited on
@senhor - you shouldn't use infinite loop(especially, when it's so easy to avoid).

@AKP - first thing - don't use global variables when you don't need to. Generally try to avoid them, they're bad programming practice ( http://stackoverflow.com/questions/484635/are-global-variables-bad ).

Well, try to run your program in your mind, like you were computer. If you don't find errors(don't know what's wrong), read my post further.

First of all, two = 90. Therefore, if you use number % two, you won't achieve what you want to(number between 1 and 100). To get number between 1 and 100, you should try:

number = (rand() % 100) + 1;
Why? Mod returns reminder. Reminder from 100 can be anything from 0 to 99. If you add 1 to the result, you will have a range of 1 to 100. If you don't understand it, try putting here different numbers, and using modulo for different numbers. Eventually, you may google it.

Secondly, you randomize "one" twice, here:

1
2
3
   one = rand() % two;
   do {
   one = rand() % two;

You could just one of these.

By the way, your code isn't readable - please try to improve your programming style. "one, two" isn't telling us anything. You could change these to "Guess" and "limit", or just leave "guess" and always mod % 100.

And well, I don't really know how can I help you. I don't know how do you want your program to work. You could either try randomizing one every loop iteration(but then it may take long time, and you don't need to change anything except for tries), or you may want to go - as your program suggests - higher or lower, according to help from user. If so, then you should change one, and stop randomizing it every loop iteration, but rather randomize it once and then increase or decrease its value.

Also, in your

1
2
3
   if(user_input == "yes") {
    ++tries;
    break;


Simpy do this:

1
2
3
4
   if(user_input == "yes") {
    ++tries;
    guessed = false;
}


And fast fixes:
- Change while loop condition, you are using operator= instead of operator==
- Define tries. It isn't automatically set to 0 - you should do it.

That's all what I can see now. I hope it helped ;)
Topic archived. No new replies allowed.