beginner problem - number guessing.

Hi all,
Am attempting the exercise where the computer has to guess a users number in 7 turns or less - am in need of a hint.

Here is the code so far - I have the basic idea for the algorithm but at present it doesn't want to play ball. Am going to stop work on this for tonight, but would greatly appreciate any pointers for when I start back on it in the morning.

- Not too much help mind ! ! just suggestions for a direction change if needed or something to consider.

Thanks to you all - really enjoying myself with these :)

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 <ctime>
#include <limits>
#include <string>
using namespace std;

int main ()
{
int guess = 50;  			//computer's guess
int x = 50;					//integer used as operator in the algorithm	
char temp [1];

cout << string( 100, '\n');
cout << "\n##################  Okay Time for me to guess your number  ###################\n";
cout << "\n\t\tPick a number between 1 and 100 and I'll guess\n";
cout << "\tPress ENTER when ready.....";

while (temp[0] != 'y')
	{
	
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	
	x = x/2;
	
	cout << "Is your number " << guess << " ? Please enter 'l' if your number is lower, 'h' for higher"
		<< " or y if I am correct:\n";
	cin >> temp[0];
	
		if (temp[0] = 'l')
		{
		guess = guess + x;
		}
			else if (temp[0]= 'h')
			{
			guess = guess - x;
			}
				else if (temp[0] = 'y')
					{
					break;
					}
	}
cout << "Not a problem! all done here, Press ENTER to quit.";
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Adios amigo!";
return 0;
	}
	
Hi,

Was very close, have managed to sort out the problem with the following code. I'm sure that i have oversimplified it a bit too much, and at present it is not robust at all.

I have 2 questions:

1) Why doesn't my 2nd call to enter() line 53 work?

2) How do i make it so that the program isn't completely messed up by illegal input - I showed it to a friend who immediately and deliberately began to put in 'unwanted' input, he then responded that the program 'does't work!' - very annoying.

Any help on either of these would be most appreciated - or on any other problems etc with my 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <ctime>
#include <limits>
#include <string>
using namespace std;

void enter()
{
cout << "\nPress ENTER to continue........";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}


int main ()
{
int guess = 50;  			//computer's guess
char temp = ' ';
int n = 0;
int array[6] = {24, 12, 6, 3, 1, 1};

cout << string( 100, '\n');
cout << "\n##################  Okay Time for me to guess your number  ####################\n";
cout << "\n\t\tPick a number between 1 and 100 and I'll guess\n";
enter();

while (temp != 'y')
	{
	
	cout << "Is your number " << guess << " ?\nPlease enter 'l' for lower, 'h' for higher or 'y' if I am correct:\n";
	cin >> temp;
	
		if (temp == 'h')
		{
		guess = guess + array[n];
		n++;
		}
			else if (temp== 'l')
			{
			guess = guess - array[n];
			n++;
			}
				else if (temp == 'y')
					{
					break;
					}
	}
cout << "Not a problem!\n\nAll done here!";
enter();

cout << "Adios amigo!";
return 0;
	}
	
	
check this out:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <ctime>
#include <limits>
#include <string>
using namespace std;

void enter()
{
    cout << "\nPress ENTER to continue........";
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}


int main ()
{
    int guess = 50;  			//computer's guess
    char temp = ' ';
    int n = 0;
    
    //I modified this a bit...
    //This way you can guarantee that the computer can find the answer
    //in at most 8 guesses, so if the user tries to continue it means
    //that he lies.
    int array[7] = {25, 12, 6, 3, 2, 1, 1};
    
    //our lie detectors :D
    int low; //a low boundary for guess
    int high; //a high boundary for guess
    bool user_lies;
    
    cout << string( 100, '\n');
    cout << "\n##################  Okay Time for me to guess your number  ####################\n";
    cout << "\n\t\tPick a number between 1 and 100 and I'll guess\n";
    enter();

    //assume that the user is trustworthy
    user_lies=false;
    
    while (temp != 'y')
    {
        cout << "Is your number " << guess 
            << " ?\nPlease enter 'l' for lower, 'h' for higher or 'y' if I am correct:\n";
        cin >> temp;
        
        //this should fix your problem with the second enter() call
        //you see, cin>>temp; leaves a '\n' pending in the input buffer
        //and then that '\n' is got by cin.ignore() in your enter()
        //so enter() doesn't w8 for the user to hit enter	   
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        //oh and it should also fix the problem with your mischievous friend :D
        
        if (temp == 'h')
        { 
            //since he picked 'h' his number is greater
            //than or equal to guess+1
            low=guess+1;
            
            guess = guess + array[n];
            n++;
        }
        else if (temp== 'l')
        {
            //since he picked 'l' his number is less
            //than or equal to guess-1
            high=guess-1;
            
            guess = guess - array[n];
            n++;
        }
        else if (temp == 'y')
        {
               break;
        }
        
        //check if the user is a liar
        if (n==8 || low>high) {user_lies=true; break;}
    }
    
    if (user_lies) cout << "You are a big bad liar!!! Shame on you...";
    else cout << "Not a problem!\nAll done here!";
    
    cout << "\nAdios amigo!" << endl;
    enter();
    return 0;
}


note that this is one possible way to check if the user lies. try to find others, be creative ;)
Last edited on
m4ster r0shi (137)

Love the adaptation - simple and effective, just as it should be!

One thing I'm not sure on is that the problem calls for the computer to find a solution in at most 7 guesses - hence my choice of numbers for the array. I figured that since i asked the user to pick any number between 1 and 100 that this excluded 1 and 100 and so the possible numbers were only 98.

If the user is allowed to choose 1 or 100 then it seems as if 8 attempts may be needed - I think i need some more puzzling.
One thing I'm not sure on is that the problem calls for the computer to find a solution in at most 7 guesses

oops, I didn't notice this... maybe if we changed the array to int array[6] = {25, 13, 6, 3, 2, 1}; it could be done in at most 7 guesses.

Of course you'll also have to change this:
if (n==8 || low>high) {user_lies=true; break;}

to this:
if (n==7 || low>high) {user_lies=true; break;}

Tell me if it worked!
Last edited on
mmm... it seems to me that it works for numbers ranging from 0 to 100, so if you don't want 0 to be a valid answer, modify the lie-checking condition so that it also accounts a guess of zero as an error (like this:
if (n==7 || low>high || guess==0) {user_lies=true; break;}), and it should be fine.
Last edited on
m4ster r0shi (149)


Many thanks for the input, I think things are pretty close to perfect now - am loving the liar failsafe!

Especially since the 'friend' was called in to re-test. He first tried inputting numbers and symbols - he was moderately impressed.

I then used the tried and tested taunt of 'Is that the best you can do!' prompting him to try lying shamelessly - the reaction to the 'YOU LITTLE LIAR!' was priceless :) followed by a how did you do that!

Ah who would have known that learning a computer language would be such fun!

thanks,

Dan

Time to move on to the next problem.......
Nearly forgot to post the final 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
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
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <ctime>
#include <limits>
#include <string>
using namespace std;

void enter()
{
cout << "\nPress ENTER to continue........";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

int main ()
{
int guess = 50;  			//computer's guess
char temp = ' ';
int n = 0;
int array[6] = {25, 13, 6, 3, 2, 1};

int low = 0; 				//a low boundary for guess
int high = 100; 				//a high boundary for guess
bool liar=false; 		//assumes truth-telling




cout << string( 100, '\n');
cout << "\n##################  Okay Time for me to guess your number  ####################\n";
cout << "\n\t\tPick a number between 1 and 100 and I'll guess\n";
enter();

while (temp != 'y')
	{
	
	cout << "Is your number " << guess << " ?\nPlease enter 'l' for lower, 'h' for higher or 'y' if I am correct:\n";
	cin >> temp;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');		// gets rid of '\n' pending in buffer
	
		if (temp == 'h')
		{
		low = guess + 1;
		guess = guess + array[n];
		n++;
		}
			else if (temp== 'l')
			{
			high = guess - 1;
			guess = guess - array[n];
			n++;
			}
				else if (temp == 'y')
					{
					break;
					}
	if (n==7 || low>high)
		{
		liar = true;
		break;
		}
	}

if (liar)
	{
	cout << "YOU LITTLE LIAR!!!!!....\n";
	}
		else
			{
			 cout << "Not a problem!\n\nAll done here!\n\nAdios amigo!!\n\n";
			 enter();
			}
return 0;
	}
	
P.S.

Only thing I had to really change was to initialise the values of 'high' and' low' to 100 and 0, without doing, the liar immediately changed to true.
I then used the tried and tested taunt of 'Is that the best you can do!' prompting him to try lying shamelessly - the reaction to the 'YOU LITTLE LIAR!' was priceless :) followed by a how did you do that!

hahahahaha, nice to know :D

Only thing I had to really change was to initialise the values of 'high' and' low' to 100 and 0, without doing, the liar immediately changed to true.

that's right, this is necessary, I totally forgot it...

Looking forward to help you with the next problem ^^
Topic archived. No new replies allowed.