So I've been working through these exercises on here, and I just finished the one where you enter a number, then it guesses it. I'm gonna post it, and I want you to point out every thing I did wrong with it. I wish to learn as much as I can from doing these practice problems.
P.S. Yes, I realize I have no input validation, and as I was writing this I also realized that I don't even need the user to enter the number, but I guess that keeps them from cheating :D
/*
This program will just guess whatever number you give it in the range
of 1 - 100. At some point, I will have it down to 7 guesses or less from
the computer.
Started on 12/15/2011
*/
//Library inclusion
#include <iostream>
#include <String>
#include <stdlib.h>
#include <time.h>
//using statements
using std::cout;
using std::cin;
using std::endl;
//Variable declarations
int userNumber;
int userResponse;
int guessedNumber;
//Function declarations
void NumberGuess(int targetNumber)
{
srand(time(NULL));
int randomNumber = rand() % 100 + 1;
int upperBound = 100;
int lowerBound = 1;
int choice;
int numberOfGuesses = 1;
bool numGuessed = false;
while(numGuessed == false)
{
cout << "Here's my guess " << randomNumber << endl;
cout << "Was my number too high, too low, or correct?\n";
cout << "1. Too high.\n";
cout << "2. Too low.\n";
cout << "3. Correct!\n";
cin >> choice;
if(choice == 1)
{
upperBound = randomNumber;
randomNumber = rand() % (upperBound - lowerBound) + lowerBound;
numberOfGuesses++;
}
elseif(choice == 2)
{
lowerBound = randomNumber;
randomNumber = rand() % (upperBound - lowerBound) + lowerBound;
numberOfGuesses++;
}
elseif(choice == 3)
{
cout << "Cool! Told you I could guess it!\n";
cout << "It only took me " << numberOfGuesses << " guesses!";
numGuessed = true;
}
}
}
int main()
{
//Dialog explaining to the user what to do
cout << "Welcome to my program! I bet I can guess any number" << endl;
cout << "you give me between 1 and 100!" << endl;
cout << endl;
cout << "So go ahead, give me a number. I'll then try to guess it." << endl;
cout << "All I need you to do is tell me too high, or too low." << endl;
//Gets user input and stores it into userNumber
cin >> userNumber;
//Runs while user did NOT enter a number between 1 and 100.
while(userNumber < 0 || userNumber > 100)
{
cout << "Sorry! I can only do numbers from 1 to 100. \n";
cout << "How about you try again?" << endl;
cin >> userNumber;
}
//Confirms number was received and guessing will commence
cout << "Cool! Thanks for the number. Now I'll begin to guess it." << endl;
//Calls function for guessing
NumberGuess(userNumber);
return 0;
}
Yea I said I didn't really need the number entered. Waste of memory, I know.
The first issue you said was brought to my attention when I friend of mine was messing with it. He tried it, and ended up causing the program to crash. Im not sure how I would fix that and still keep the program from seemingly not knowing their number.
You guess 20.
They say it's too low.
You guess 21.
They say it's too high.
What does this tell you? n > 20 && n < 21. That's impossible. If this was the case, what would your upper and lower bounds be at this point and how could you check?
I guess a better algorithm would be to always keep your guess as the average of the upper bound and the lower bound... That way, you're bound to guess it much quicker than guessing random numbers while shortening your range... Like, for example:
Suppose their number is 51:
You guess 30: Too low.
You guess 31: Too low.
You guess 32: Too low.
.
.
.
Get the idea..? I know this is unlikely to happen, but basically, the number of guesses is more. This way, your range will reduce by a large amount after every guess... Just a suggestion.. :)
Hmm, thank you Caprico. I was actually trying to figure out a way to cut down on my number of guesses. The most it took was 8, but this sounds like it will cut it down quite a bit.
So...
Your range is basically 1-100. So considering that each guess is higher than the actual number:
50->25->13->7->4->2.
At this point you know the answer is either 1 or 3. This is your last and correct guess (so technically 7 guesses).
Also, since the number can either be higher or lower, the problem is symmetric about any choice. Hence no matter what the number is, the program will guess it correctly at the 7th guess (max). :)