0

As an exercise, I'm trying to make a program where the computer tries to guess which number I am thinking of. I am able to make it give random guesses in general but I want to make it able to guess the right number more quickly by recording it's highest and lowest guesses and making new guesses in-between those.

For example, if the computer guesses 20 then 40, it's third guess should only be between 20 and 40. Is this possible and if so what am I doing wrong? #include #include #include using namespace std;

int main() { string yesOrNo; string lowOrHigh; srand(time(NULL)); int random_number = 1+(rand() % 100); int lowGuess(0); int highGuess(100); int guessAttempts(1); int adjustMinMax(int random_number, int& highestGuess, int& lowGuess, string lowOrHigh); int adjustGuess(int& random_number, int lowGuess, int highestGuess); cout << "Think of a random number between 1 and 100 in your head."; cout << endl << "Guesses Attempted: " << guessAttempts; cout << endl << "Is your number " << random_number << "?"; cout << endl << "y/n: "; cin >> yesOrNo; while (yesOrNo == "n" || yesOrNo == "no") { ++guessAttempts; cout << endl << "Was my guess too high or too low l/h? "; cin >> lowOrHigh; adjustMinMax(random_number, highGuess, lowGuess, lowOrHigh); adjustGuess(random_number, lowGuess, highGuess); cout << "Guesses Attempted: " << guessAttempts; cout << endl << "Is your number " << random_number << "?"; cout << endl << "y/n: "; cin >> yesOrNo; } cout << "Hooray! :)"; return 0; } int adjustGuess(int& random_number, int lowGuess, int highGuess) { random_number = rand() % (highGuess - lowGuess + 1) + lowGuess; return (random_number); } int adjustMinMax(int random_number, int& highGuess, int& lowGuess, string lowOrHigh) { if (lowOrHigh == "l" || lowOrHigh == "low") { return(lowGuess = random_number); } if (lowOrHigh == "h" || lowOrHigh == "high") { return(highGuess = random_number); } } 
4
  • 1
    The expression yesOrNo == 'no' should make the compiler shout warnings at you. Commented Sep 12, 2015 at 20:14
  • I'm fairly new to programming so I must ask. Why should it? Commented Sep 12, 2015 at 20:16
  • A character is a character, singular. You have two characters in that literal. Commented Sep 12, 2015 at 20:19
  • Oh I see, so should yesOrNo be string? Commented Sep 12, 2015 at 20:20

2 Answers 2

0

Adjusting the min and the max has to be done before the game makes a new guess. Instead of:

adjustGuess(random_number, lowOrHigh, lowestGuess, highestGuess); adjustMinMax(random_number, highestGuess, lowestGuess); 

try:

adjustMinMax(random_number, highestGuess, lowestGuess); adjustGuess(random_number, lowOrHigh, lowestGuess, highestGuess); 

Also adjustMinMax has to know whether the previous guess was too high or too low, and should use that information to decide which to change.

Another problem is your adjustGuess function does not seem to do what you think it does. It should be ambivelant to whether the previous guess was too high or too low, and when picking a new random number it should consider the highestGuess and the lowestGuess. Try something like:

random_number = lowestGuess + rand() % (highestGuess - lowestGuess); 

There are some other problems too, like yesOrNo has to be a string if you want it able to store "no".

See what happens if you fix those problems.

Sign up to request clarification or add additional context in comments.

7 Comments

I tried this and It gave me an error "Division by zero"
On what line? If its in the line with "% (highestGuess - lowestGuess);" try adding "if(highestGuess !=lowestGuess". If the two are equal, set random_number to lowestGuess (or highestGuess).
After trying this I no longer get the error, but the program still doesn't seem to be doing what I want it to
I don't understand what you are trying to do. adjustGuess should not take lowOrHigh as input. Adjust min and max must take low or high as input. You say you tried my changes, but I don't think you really did.
adjustGuess should not care whether the prvious answer was too high or too low, it only cares what the highest answer called 'l' was, and what the lowest answer called 'h' was.
|
0

The expression yesOrNo == 'No' is comparing a char variable to a string of characters which is not allowed. Also, if the user enters no the cin object will take the 'n' and leave the 'o' and a '\n' in the input buffer. Then next time you ask for input the cin will take the 'o' and the '\n'which is still in the buffer. you should cin.ignore() to remove the extra char from the buffer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.