c - Fgets 和 sscanf 不等待输入 C

标签 c fgets scanf

最初我使用的是 scanf,但我遇到了换行符卡在标准输入中的情况。我读到的所有内容都是说切换到 fgets 并使用 sscanf 代替。有了这个,我决定改用这个……但这仍然不起作用。您将在下面找到我的代码。我的问题是,我的 fgets 和 sscanf 做错了什么导致它不等待用户输入?

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

typedef struct f_in{
    char outline;
    int lines;
    int rows;
    int num_args;
} F_IN;

typedef struct args_in {
    char in_string[20];
    int t_format;
} ARGS_IN;

void printInterface(char argQs[5][50], char argChar);

int main(int argv, char** argc){
    char defaultQuestions[5][50] = { { "1) What char for border?" }
            , { "2) Add question" }
            , { "3) Remove Question" }
            , { "4) Print last answers" }
    , { "5) Exit" } };
    int commandEntry, exitFlag;
    char borderChar = '*', addQ[50],userInp[1];

    exitFlag = 1;

    while (exitFlag){
            printInterface(defaultQuestions, borderChar);

            printf("Enter the integer value for the command you wish to select:  ");
            fgets(userInp, sizeof(userInp),stdin);
            sscanf(userInp,"%d", &commandEntry);
            printf("\nYou selected: %s\n", defaultQuestions[commandEntry - 1]);

            userInp[0] = 0;

            if (commandEntry == 1){
                    printf("Please enter the character you wish to be the border:  ");
                    fgets(userInp,sizeof(userInp),stdin);
                    sscanf(userInp,"%c",&borderChar);
            }
            else if (commandEntry == 2){
                    printf("What question would you like to add? (only enter 50 char max)\n");
                    fgets(addQ, 50, stdin);
                    printf("This was your question: %s", addQ);
            }
            else if (commandEntry == 5){
                    printf("Goodbye!\n");
                    exitFlag = 0;
            }
    }
    return 0;
}

void printInterface(char argQs[5][50], char argChar){
    int i, j;
    int lineCnt = 13;
    int borderLen = 75;

    for (i = 0; i<100; i++){
            printf("\n");
    }

    for (i = 0; i<lineCnt; i++){

            if (i == 0 || i == lineCnt - 1){
            for (j = 0; j<borderLen; j++){
                            printf("%c", argChar);
                    }
                    printf("\n");
            }

            else if (i >= 3 && i <= 10){
                    printf("%c    %s", argChar, argQs[i - 3]);
                    for (j = 0; j < ((borderLen - strlen(argQs[i - 3]))-6); j++){
                            printf(" ");
                    }
                    printf("%c\n", argChar);
            }

            else{
                    for (j = 0; j<borderLen; j++){
                            if (j == 0){
                                    printf("%c", argChar);
                            }
                            else if (j == (borderLen - 1)){
                                    printf("%c\n", argChar);
                            }
                            else{

                    for (j = 0; j<borderLen; j++){
                            if (j == 0){
                                    printf("%c", argChar);
                            }
                            else if (j == (borderLen - 1)){
                                    printf("%c\n", argChar);
                            }
                            else{                
                                    printf(" ");
                            }
                    }
            }        
    }                  

    for (i = 0; i<10; i++){ 
            printf("\n");
    }

}

最佳答案

“userInp[1]仅允许足够的内存来存储终止'\0'” - 用户312023

关于c - Fgets 和 sscanf 不等待输入 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40348325/

相关文章:

c - 将换行符添加到 printf() 是否等同于刷新流?

c - 输入 EOF 时 Shell 无限循环 C

c - C 中最快的 fgets 实现

c - 从 C 语言的文本文件中读取 IP 地址

c - 如何将 pthread_t id 保存到数组

c - 如何使用 C 中的 libxml 删除 XML 中元素/节点之间的空格?

c - 在 Linux 中使用 ftok() 最多可以创建多少个共享内存 key ?

c - 在 C 编程中打开文件并将文件数据更改为大写

c - 将 CSV 读入数组以按行中给出的数字进行数字排序

arrays - 如何使用 scanf 将元素接收到数组中?