Limiting Variables

I am working on a project for my computer science class, and I have to make a guessing game, but the computer is doing the guessing. The user picks a number from 1-100 as their number and the program must guess based on whether the user says it is higher or lower. I am having trouble re-limiting the max and min value after each guess. Ex. if my # is 66, I press 2 for higher(changing to h/l after), and it seems to limit it to 51-100, but after that the limiting isn't working very well. I think the problem might be in line 44 with lowering, I'm not sure.

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
	int i;
	int max = 100;
	int min = 1;
	int compguess = 50;
	int numguess = 1;
	float answer;
	compguess<max;
    compguess>min;
    srand ( time(NULL) );

	cout << "Press 0 after guessing a number to exit the program." << endl;
	cout << endl << "Please enter a number, I will attempt to guess the number." << endl;
	cin >> i;

	do
	{
        cout << "Is your number " << compguess << "? " << endl;
        cout << "If it is, press 1. If it is higher than 50, press 2. If it is lower than 50, press 3." << endl;
        cin >> answer;

        if (answer==1)
        {
            cout << "Your number was " << compguess << "! I guessed in " << numguess << " turns." << endl;
        }

        if (answer==2)
        {
            min = i;

            compguess = rand() % (100-min) + min;
            cout << "My guess is: " << compguess << endl;
            numguess++;
        }
         else if (answer==3)
        {
            max = i;
            compguess = rand() % (50-min) + min;
            cout << "My guess is: " << compguess << endl;
            numguess++;
        }
	}
	while (i!=0, answer!=1);
	return 0;
}


What is wrong with the code? Everything else seems to work so far.
your problem is with
compguess = rand() % (50-min)+min;

if you change it to

compguess = rand() % 50;

it will work just fine. by doing it this way your only letting it guess number 0-50.
you should change in this way:
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
do{
    cout << "Is your number " << compguess << "? " << endl;
    cout << "If it is, press 1. If it is higher than "<<compguess<<", press 2. If it is lower than "<<compguess<<", press 3." << endl;
    cin >> answer;

    switch(answer){
        case 0:break;
        case 1:{
                cout << "Your number was " << compguess << "! I guessed in " << numguess << " turns." << endl;
            break;
        }
        case 2:{
            max=compguess-1;
            if(max-min <=0)break;
            compguess = rand() % (max-min+1) + min;
            cout << "My guess is: " << compguess << endl;
            numguess++;
            break;
        }
        case 3:{
            min=compguess+1;
            if(max-min <=0)break;
            compguess = rand() % (max-min+1) + min;
            cout << "My guess is: " << compguess << endl;
            numguess++;
            break;
        }
        default:{
            cout<<"wrong choice!";
            break;    
	}
    }
    if(max-min<=0){
        cout<<"I HAVE guessed, you lied me!";
        break;
    }
}while(answer!=0);
Last edited on
@ k0t4, that only helps the second guess, after that it won't help.

@ vins3xtreme, i tried replacing my code with that and it isn't working very well... any other help please?
maybe a function that generate the number between min and max :
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
int guessNumber(int min,int max);
void main(){
    int min=1,max=100,compguess,numguess=0,int answer;
    do{
        compguess = guessNumber(min,max);
        cout << "My guess is: " << compguess << endl;
        numguess++;
        cout << "Is your number " << compguess << "?\n ";
        cout << "If it is, press 1. If it is higher than "<<compguess<<", press 2. If it is lower than "<<compguess<<", press 3.\n";
        cin >> answer;
        switch(answer){
            case 0:
            case 1: break;
            case 2:{
                max=compguess-1;
                compguess = rand() % (max-min+1) + min;
                cout << "My guess is: " << compguess << endl;
                numguess++;
                break;
            }
            case 3:{
                min=compguess+1;
                break;
            }
            default:{
                cout<<"I think another number...";
                break;    
	    }
        }
        if(min>max)break;
        compguess = guessNumber(min,max);
        cout << "My guess is: " << compguess << endl;
        numguess++;
    }while(answer!=0);
if(max>=min) cout << "Your number was " << compguess << "! I guessed in " << numguess << "turns.\n";
else cout<<"I HAVE guessed! the number must be "<<compguess<<", you lied me!";
}

int guessNumber(int min,int max){
    if(min>=max)return min;
    else return (rand() %(max-min) +min;
}
Last edited on
Topic archived. No new replies allowed.