Fgets和sscanf未等待输入C

Fgets和sscanf未等待输入C,c,fgets,scanf,C,Fgets,Scanf,最初我使用的是scanf,但是我遇到了新行字符被困在stdin中。我读到的所有内容都是说,切换到fgets,改用sscanf。有了这个,我决定换成那个……但那个仍然不起作用。下面是我的代码。我的问题是,我的fgets和sscanf有什么问题导致它不等待用户输入 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct f_in{ char outline;

最初我使用的是scanf,但是我遇到了新行字符被困在stdin中。我读到的所有内容都是说,切换到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");
    }

}
#包括
#包括
#包括
类型定义结构f_in{
字符轮廓;
内线;
int行;
int num_args;
}福因;
中的typedef结构参数{
字符串中的字符[20];
int t_格式;
}ARGS_IN;
void打印接口(char argQs[5][50],char argChar);
int main(int argv,字符**argc){
char defaultQuestions[5][50]={{{“1)什么字符表示边界?}
,{“2)添加问题”}
,{“3)删除问题”}
,{“4)打印最后的答案”}
,{5)退出};
int commandEntry,exitFlag;
char borderChar='*',addQ[50],userInp[1];
exitFlag=1;
while(exitFlag){
打印接口(默认问题、边界字符);
printf(“为要选择的命令输入整数值:”;
fgets(userInp、sizeof(userInp)、stdin);
sscanf(userInp、%d、&commandEntry);
printf(“\n您选择了:%s\n”,defaultQuestions[commandEntry-1]);
userInp[0]=0;
if(commandEntry==1){
printf(“请输入希望作为边框的字符:”;
fgets(userInp、sizeof(userInp)、stdin);
sscanf(userInp、%c、&borderChar);
}
else if(commandEntry==2){
printf(“您想添加什么问题?(最多输入50个字符)\n”);
fgets(addQ,50,标准DIN);
printf(“这是您的问题:%s”,addQ);
}
else if(commandEntry==5){
printf(“再见!\n”);
exitFlag=0;
}
}
返回0;
}
void打印接口(char argQs[5][50],char argChar){
int i,j;
int-lineCnt=13;
int-borderLen=75;
对于(i=0;i“userInp[1]只允许足够的内存来存储终止的'\0'”

-user312023

到底什么不起作用?输入是什么?预期的输出是什么?修改你的问题并说明清楚。@chux,我之所以这样做是因为当时我认为,因为我只需要接受数字1-5作为输入,一个字符数组就足够了。我完全忘记了e空终止符,因此出现了问题。发布的代码包含多个“魔法”数字。魔法数字是没有基础的数字。例如,5,50建议使用
enum
语句或
#define
语句为这些“魔法”数字指定有意义的名称,然后在整个代码中使用这些有意义的名称。此行:
fgets(userInp,sizeof(userInp),stdin);
实际上是说输入0个字符,因为
fgets()
始终在输入字段数据的末尾追加一个NUL字符,由于输入字段只有1个字符长,因此导致输入字段
userInp
的所有内容都是NUL字符。代码块以以下语句开头:
if(commandEntry==1){
最好合并到
switch()
语句的case语句中。它们应该是最终测试(默认语句),向用户打印关于无效输入的消息