1
votes

I am trying to make a program where the computer has to guess a number between 0 and 100 picked by the user(me). The program should guess the number within 7 tries which it does, but I'm having a problem with scanf(). When the program tries to guess the number, the user must tell it if the number guessed is too high, too low, or correct. The program works fine when the user just types a one character response, but freaks out when there is more than one character in "response". So I would like to limit the response to just one character. How do I do this?

Thanks

#include <stdio.h>
#include <string.h>

int main() {
    char response[1];
    int numOfGuesses = 1;
    int min = 0;
    int max = 100;
    int guess = (max+min)/2;
    int end = 0;

    do {
        printf("%d) %d: (h)igh, (l)ow, or (c)orrect? ", numOfGuesses, guess);
        if (scanf("%1s", response) == 1) {

            printf("%s \n", response);
            if (strlen(response) > 1) {
                printf("D'oh! Wrong response!! \n");
            } else {
                if (response[0] == 'h') {
                    max = guess;
                    numOfGuesses++;
                } else if (response[0] == 'l') {
                    min = guess;
                    numOfGuesses++;
                } else if (response[0] == 'c') {
                    printf("Correct! It took %d turns. \n", numOfGuesses);
                    end = 1;
                } else {
                    printf("D'oh! Wrong response! \n");
                }

                guess = (max+min)/2;
            }

        } else {
            end = 1;
        }

    } while ( end == 0 );

    return 0;
}
4
Why aren't you just using a single char instead? - Richard J. Ross III

4 Answers

4
votes

Your response buffer is not null terminated, which pretty much makes it explode at

if (strlen(response) > 1) {

Make it response[2].

Alternatively, you can use %c in scanf instead of %1s.

Edit:

Try calling this function after every scanf, it flushes the input buffer until the next newline, removing any invalid input after the first char.

void flush_input()
{
    int c = 0;

    do 
    {
        c = getchar();
    } while (c != EOF && c != '\n');
}
1
votes

If you're just looking for a single character input, why not use getchar() instead of scanf()?

Here's a page that discusses getchar() in more detail and has some usage examples, if you're interested: http://rabbit.eng.miami.edu/class/een218/getchar.html

0
votes

Are you getting extra points for saving memory?

#include <stdio.h> up top, then declare response to be a large-ish array: char response[BUFSIZ];

You should initialize it before the loop, just because it's a good habit: *response='\0';

As you're not doing much, you could call gets(response) instead of scanf(). This will read in the entire line entered. Modern compilers and/or run-times will whine about the gets() as it can run off the end of the provided array (which is what your scanf() was doing), suggesting fgets() instead. The more-correct snippet becomes:

fgets(response, sizeof response, stdin);
0
votes

use

if (scanf("%2s", response) == 1) {

instead of

if (scanf("%1s", response) == 1) {

this makes the compiler read up to 2 characters instead of just 1 at a time