fgets和清除输入缓冲区不能读取用户输入

时间:2016-10-16 20:10:38

标签: c

我的程序相当简单。

读取用户的H,W,水平字符,垂直字符。

然后用给定的参数打印出正方形。

但是,当用户输入'y'时,第二次运行。

高度参数总是丢失。

我没理解。

我可以想到的一个可能的错误点是输入缓冲区可能不会清理。

有什么想法吗?

C程序

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>

const int USER_INPUT_SIZE_EACH_LINE = 999;

void draw_middle_lines(char *middle_line, int width) {
    printf("%s", middle_line);
    for (int i = 2; i < width; i++) {
        printf(" ");
    }
    printf("%s\n", middle_line);
}

void draw_bound_line(char *bound_char, int width) {
    for (int i = 0; i < width; i++) {
        printf("%s", bound_char);
    }
    printf("\n");
}


void clearNewline(char *line, int max_len) {
    for (int i = 0; i < max_len; i++){
        printf("%c", line[i]);
    }
    printf("\n");
    for (int i = 0; i < max_len; i++) {
        if (line[i] == '\n') {
            line[i] = '\0';
        }
    }
}

char **ask_user_preference() {
    printf("\nask user preference\n");
    const int NUM_OF_PROMPTS = 4;
    const char *prompt[] = {
            "please input height:",
            "please input width:",
            "please input horizontal char:",
            "please input vertical char:",
    };
    char **preference;
    preference = malloc(NUM_OF_PROMPTS);
    for (int i = 0; i < NUM_OF_PROMPTS; i++) {
        preference[i] = malloc(USER_INPUT_SIZE_EACH_LINE);
        memset(preference[i], 0, sizeof(char) * USER_INPUT_SIZE_EACH_LINE);
        printf("%s", prompt[i]);
        fgets(preference[i], USER_INPUT_SIZE_EACH_LINE, stdin);
        clearNewline(preference[i], USER_INPUT_SIZE_EACH_LINE);
    }
    return preference;
}

void draw(int w, int h, char *bound_char, char *middle_char) {
    printf("width: %d\n", w);
    printf("height: %d\n", h);
    draw_bound_line(bound_char, w);
    for (int i = 2; i < h; i++) {
        draw_middle_lines(middle_char, w);
    }
    draw_bound_line(bound_char, w);
}

void clr_input_buffer() {
    for (;;) {
        int c = getchar();
        if (c == EOF || c == '\n')
            break;
    }

}

int main() {
    char *cont = malloc(10+1);
    do {
        char **pref = ask_user_preference();
        int height = atoi(pref[0]);
        int width = atoi(pref[1]);
        char *bound_char = pref[2];
        char *middle_char = pref[3];
        draw(width, height, bound_char, middle_char);
        printf("continue to play (y/n)? ");
        fgets(cont, 2, stdin);
        if (strncmp(cont, "y", 1) == 0) {
            clr_input_buffer();
        }else{
            break;
        }
    } while (1);
    printf("BYE BYE~");
    return 0;
}

0 个答案:

没有答案