Program where the computer guesses my number..

I am trying to solve a problem wherein the computer must guess the number i had in mind. I come up with this solution:

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<stdlib.h>
#include<time.h>
#include<string>

using namespace std;

int main(){
	int x,y,z, terminator=0,count;
	string user[3]={"lower","higher","correct"};
	string me;
	srand(time(NULL));
	y=100;
	z=1;
	cout<<"Pick a number between 1 - 100 and I will try to guess it.\n";
	while(terminator==0){
		int num=3;
		x=rand()% y+z;
		cout<<"is it "<<x<<" ?";
		cin>>me;
		for(count=0;count<3;count++){
			if(me==user[count]){
				num=count;
			}
		}
		switch(num){
			case 0:
				y=x;
				break;
			case 1:
				z=x;
				y=y-z;
				break;
			case 2:
				cout<<"hahaha! I told you I can guess it.";
				terminator++;
				break;
			default :
				cout<<"what was that?\n";
				break;
		}	
	}

	return 0;
}


but encountered a problem.. well it is a problem I can't quite understand. hope you can help me.. thanks..

PS. i have a guess that my problem is my math. am i right?

but encountered a problem.. well it is a problem I can't quite understand. hope you can help me.. thanks..

You'd better tell exactly what your problem is. Telepathes are on vacations, you know.


x=rand()% y+z;

i.e if z = 40 and y = 90, then x could be 129?
was this exactly what you want?
you need rand of their difference added to smaller of them. however you'd better assign to x the average of y and z. Rand is not necessary here...
Last edited on
made an upgrade here..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
		switch(num){
			case 0:
				y=x;
				break;
			case 1:
				y=(y+z)-x;
				z=x;
				break;
			case 2:
				cout<<"hahaha! I told you I can guess it.";
				terminator++;
				break;
			default :
				cout<<"what was that?\n";
				break;
		}	


but still wrong.. i dont know.. :C i'm lost
Last edited on
Topic archived. No new replies allowed.