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?
#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;
}