Ive been trying to create a number guessing program were the program guesses your number instead of the user guessing the programs number heres what i have so far:

#include <stdio.h>

int main(void)

{

    int guess = 50;


    printf("Pick an integer from 1 to 100. I will try to guess ");

    printf("it.\nRespond with a y if my guess is right and with");

    printf("\nan n if it is wrong.\n");

    printf("Uh...is your number %d?\n", guess);

    while (getchar() != 'y')      /* get response, compare to y */
{
        printf("Well, then, is it %d?\n", ++guess);
}
    printf("I knew I could do it!\n");



    return 0;

}

Now basically all i have to do is modify the program so that it will for ex. say, the
guess is lower, the next guess be halfway between 50 and 100, which is, 75. If that guess is higher, the next guess will be halfway between 75 and 50 which will be 63 since (50 + 75) / 2 = 63. Apparently im supposed to use the binary search strategy. Also i have to allow 1-character response like H or L instead of high or low.

Any Help will be greatly appreciated thanks.

Recommended Answers

All 2 Replies

Your program says I can pick from between 1 and 100. What would happen if I pick 6 or 12 or any thing below 50?
Probably I'll get old clicking that darn n (no) key.

Next time use proper tagging to post your code.

[edit:]
You have a problem here as well. while (getchar() != 'y') When you enter a N/n for no, the input buffer records two key strokes; the actual key and the ENTER key, in next turn the while loop doesn't even ask you because already contains ENTER.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.