Computer guesses number

I needed to write a program to ask the user to think of a number between 1 and 100, then the computer would guess until the correct number was found... This is what I have written, I can't understand why it doesn't work. When I enter down(D) after entering up(U) (or the opposite) the program just starts spitting out random numbers that are sometimes not even in range! Any Clues?
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
#include <conio.h>
#include <iostream.h>
#include <math.h>

int main()
{
    srand (time(0));
    int start;
    char userenter;
    int guess=50;   //initializes guess at 50
    int lowest=0;   //initializes lowest possible guess at 0
    int highest=100; //initializes highest possible guess at 0
    
    
    
    cout<<"Think of a number between 1 and 100..."<<endl<<endl;
    
    
    while(start!=0)
    {
    cout<<"Enter 0 to start: ";
    cin>>start;
    }
    system("CLS");   //clears screen
    
    
    while(userenter!='Y' && userenter!='y')
    {
    cout<<"IS THE NUMBER "<<guess<<"?... Y for yes, U for up, D for down"<<endl;
    cin>>userenter;
    
         //if the number is higher
         if(userenter=='u'||userenter=='U')
         {
         lowest=guess;  
         guess=rand()% highest+lowest;
         }
         
         //if the number is lower
         if(userenter=='d'||userenter=='D')
         {
         highest=guess;
         guess=rand()% highest+lowest;
         }
    }
         
    getch();
    return 0;
}

Try changing guess=rand()% highest+lowest; to guess=rand() % (highest - lowest + 1) + lowest;.
Last edited on
Thank You!! That works!
A more logical 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
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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int start = 1;
    char userEnter = 's';
    int change = 25;   //the number to change the guess by
    int guess = 50;    //initializes guess at 50
    int lowest = 0;    //makes lowest possible guess 0
    int highest = 100; //makes highest possible guess 100
    
    while(start!=0)
    {
          cout<<"Enter 0 to start: ";
          cin>>start;
    }
    system("CLS");   //clears screen
    
    cout<<"Think of a number between 1 and 100..."<<endl;

    while(userEnter!='Y' || userEnter!='y')
    {
	cout<<"IS THE NUMBER "<<guess<<"?... Y for yes, U for up, D for down"<<endl;
	cin>>userEnter;
    
        //if the number is higher
        if(userEnter =='u'||userEnter =='U')
        {
	     guess += change;
	     change /= 2;
        }
         
        //if the number is lower
        if(userEnter=='d'||userEnter=='D')
        {
	     guess -= change;
	     change /= 2;
        }

		if(guess < 0) //not lower than 0
		{
		          guess = 0;
		}

		if(guess > 100) //not higher than 0
		{
			guess = 100;
		}

		if(change == 12.5) //avoid decimals
		{
			change = 12;
		}

		if(change == 1.5) //avoid decimals again
		{
			change = 1;
		}

		if(change == 1/2) //and again
		{
			change = 1;
		}
	}
     
	return 0;
}


Neaten the spacing and it should be good!!

- Kyle
I just wrote this program up a couple months ago. You actually don't need a variable at all for the users number if you think about.
Topic archived. No new replies allowed.