Hello kmce,
Your first task is to understand how the game works. Based on your code I do not feel that you do.
Lets say the number to find is 42. The computers first guess would be 50 which you tell it that it is to high. The next guess would be half of 50 or 25 which you tell it that it is to low. The next guess would be 37 which you say is to low. Next is half way between 37 and 50 or 43 which you say is to high. Next is half way between 37 an 43 or 40 which is to low. The next guess is half way between 40 and 43 or 41 which is to low. the last guess is between 41 and 43 or 42 which is correct.
If I counted right that would be seven tries.
Understanding how the game works you can write code for the computer guessing the number or the user guessing the number.
Your program is a good start and
zapshe has one solution that can help.
Starting with your code:
First post the whole program. As is there is no way to test you code without guessing at the header files that should be there.
The first variables are usable, but looking at
zapshe's code "low" and "high" are a better choice and easier to understand.
"temp" is never used and you can delete it.
For your "char" avoid using a single letter variable. Give it a name that means something. The best choice is a noun that describes what it is or what it does. I would consider something like "lowHigh" because that is what you are checking for.
Line 6 is a prompt for the user to enter something. I would write the line as
cout << "\n Is your number greater than 50: ";
. The "\n " at the beginning is what I have come to use most often. Ending the string with the ": " keeps the input on the same line and tends to make more sense this way.
Between lines (6) and (7) you missed the "std::cin" to get any type of input from the user.
Line 7 is checking an uninitialized variable which has a garbage value against "y". It would be a very rare occasion for this to be a true statement.
Another thing you are working on is that the user can follow directions. Do not count on this.
There are three options that you can use:
1 2 3 4 5 6 7 8
|
std::cin>>lowHigh;
lowHigh = std::tolower(lowHigh); // <--- Needs header file <cctype>
if (std::tolower(lowHigh) == 'y')
or
if (lowHigh == 'y' || lowHigh == 'Y')
|
Any one would work. For the "std::tolower()" or the opposite "std::toupper()" you will need the header file "<cctype>". If you are not familiar with it have a look at
http://www.cplusplus.com/reference/cctype/ There is a nice table showing the ascii codes from 0 to 255 or 0x00 to 0x7F and what functions can be used with each character in the table along with all the functions that are available.
That said what you are really looking for is "l" (low) or "h" (high) not "y" for yes.
I do agree with
zapshe you do need a loop. This could be a for loop, a while loop or even a do/while loop. All can work. For a for loop I would consider this
for (tries = 0; !win && tries < 7; tries++)
Since the point is that the number can be guessed in 7 tries or less the for loop needs to account for this. Also by defining "tries" outside of the for loop yo can use the final value after the for loop if yo need it. I am thing something like:
std::cout << "\n The computer took " << tries << " to guess the number"<< std::endl;
. You could even use an if/else to print a different message should "tries" exceed seven.
I leave the rest to see what you come up with.
Hope that helps,
Andy