Im trying to do the beginner exercise called Bracketing Search.
Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretely decided. The user must tell the computer whether it guesed too high or too low.
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
int randomNumber;
int guess, userInput;
int numberOfGuesses = 0;
bool Quit = false;
bool QuitTwo = false;
while(!QuitTwo)
{
cout << "Enter a number between 0 and 100 that the computer must guess" << endl;
cin >> randomNumber;
if(randomNumber >= 0 && randomNumber <= 100)
{
QuitTwo = true;
}
}
srand(time(0));
for(guess = 0; guess < 1; guess++)
{
guess = (rand()%100);
}
while(!Quit)
{
cout << "\nThe computers guess is " << guess << endl;
cout << "\nIf the guess is too low press (1)" << endl;
cout << "If the guess is too high press (2)" << endl;
cout << "Tf the guess is correct press (3)" << endl;
cin >> userInput;
switch(userInput)
{
case 1:
if(guess < randomNumber)
{
guess++;
numberOfGuesses++;
}
break;
case 2:
if(guess > randomNumber)
{
guess--;
numberOfGuesses++;
}
break;
case 3:
if(guess == randomNumber)
{
numberOfGuesses++;
Quit = true;
if(Quit == true)
{
cout << "\nThe computer has guessed correct" << endl;
cout << "The right number is " << randomNumber << endl;
cout << "The computer used " << numberOfGuesses << " tries" << endl;
}
}
break;
default:
break;
}
}
return 0;
}
My problem is that i don't know how to make the computer smarter :P
I need it to do something else than ++ and --, when it guesses wrong.
Would really appreciate some help.