ok so the computers supposed to guess my number however when i go to run it it asks what my repsonse to its guess is and shuts down. Anyone that can help? I would much appreciate it :)
/* variables */
int min, max, guess;
min = 1;
max = 100;
guess = (max + min / 2);
/* Asks the user to pick a number*/
printf("Think of a number in the range of 1-100\n");
/* Applys first guess */
printf("My guess is %d, whats your response? (>,<, or =)\n", guess);
while (response != '=')
printf("My guess is %d, whats your response? (>,<, or =)", guess);
if (response == '>')
min = ( max + min / 2);
if (response == '<')
max = ( max + min / 2);
After you type your response of <, > or =, you press enter. That enter key is actually a key, so you get a new line ('\n') in your input buffer, which scanf reads when you use "%c". If you put a space before the %c, it will skip any leading white space, including the new line. Also, you need to provide the address of the response variable, so scanf knows where to store the result. If your compiler doesn't complain about that, then turn up the warnings or get a better compiler. Your scanf calls should look like this:
And just an FYI, system("pause"); will only work on Windows, so you run the risk of alienating people here that use Linux, who could otherwise help you with your problems. There are other ways to keep your console window from disappearing. Read this link: FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com.
scanf(" %c", &response);
EDIT: Also, you could (IMO) improve the structure of your program a bit by using a do-while loop (so you only have to prompt the user in one place), and using an if-else if structure for comparing the response:
print "my guess is ... what's your response..."
if (response == '<')
else if (response == '>')
else if (response == '=')
print "invalid input"
} while (response != '=');
I think it's time for a new FAQ entry. :-)
I think you just volunteered to write it! I look forward to linking newbies to it.
Originally Posted by AndiPersti
Frankly, I've thought about it for some days now.
Probably will have some time tomorrow to write it down :-)
You have some logic problems.
If the response is > the guess, you want the min to be assigned the value of guess + 1. If the response is < the guess, you want the max to be assigned to guess - 1.
Outside the if(response) statement, you have the line of code, guess = (min+max)/2.