fgets不';C中的t工作(标准输入看起来已满)

fgets不';C中的t工作(标准输入看起来已满),c,buffer,stdin,fgets,C,Buffer,Stdin,Fgets,这是我的密码,请看一下。我的菜单不断循环,不要求任何输入。我不知道为什么fgets不能按预期工作 当我运行代码时,它将继续循环,即使当我摆脱循环时: int main () { char input[3]; char opt1; int flag=1,n; /*hrtime_t start, end;*/ dlist_t lst; list_init (&lst); list_input (&lst); bub

这是我的密码,请看一下。我的菜单不断循环,不要求任何输入。我不知道为什么
fgets
不能按预期工作

当我运行代码时,它将继续循环,即使当我摆脱循环时:

int main ()
{ 
    char input[3];
    char opt1;
    int flag=1,n;

    /*hrtime_t start, end;*/
    dlist_t lst;

    list_init (&lst);
    list_input (&lst);
    bubblesort (&lst);

    list_display(&lst);

    while(flag == 1)
    {
        printf("\nMain Menu\n");
        printf("-----------\n");
        printf("1. Bubble Sort\n");
        printf("2. Selection Sort\n");
        printf("3. Quick sort\n");
        printf("4. Merge Sort\n");
        printf("5. Exit\n");

        printf("\nEnter your option[1-5]: ");

        fgets(input, 3, stdin);
        opt1 = input[0];

        /* If condition to display the main menu if user inputs enter */
        if(opt1 == '\n')
        {
            flag =1;
            continue;
        }

        n = strlen(input)-1;

        if(input[n] == '\n')
        {
            input[n] = '\0';
        } else {
            printf("\nInvalid input. ");
            printf("Please note that the maximum length of the input is 1.");
            readRestOfLine();
            flag =1;
            continue;
        }

        switch(opt1)
        {
            case '1':
                /*start = gethrtime();
                  bubbleSort(list);
                  end = gethrtime();*/
                printf("\nBubble Sorted List\n");
                break;
            case '2':
                /* start = gethrtime();
                   selectionSort(list);
                   end = gethrtime(); */
                printf("\nSelection Sorted List\n");
                break;
            case '3': 
                /*start = gethrtime();
                  quickSort(list, 0, list->list_len-1);
                  end = gethrtime(); */
                printf("\nQuick Sorted List\n");

                break;
            case '4':
                /*start = gethrtime();
                  list->head = mergeSort(list->head);
                  mergeSortReverse(list);
                  end = gethrtime(); */
                printf("\nMerge Sorted List\n");
                break;
            case '5':
                SNExit();
                list_free (&lst);
                printf("\n\n  ********* THANK YOU **********");
                return EXIT_SUCCESS;
            default :
                {
                    printf("\nPlease enter valid option\n");
                    break;
                }
        }
    }
    return EXIT_SUCCESS;
    return 0;
}

/****************************************************************************
 * Function readRestOfLine() is used for buffer clearing.
 ****************************************************************************/
void readRestOfLine()
{
    int c;

    /* Read until the end of the line or end-of-file. */
    while ((c = fgetc(stdin)) != '\n' && c != EOF)
        ;
    fflush(stdin);
    /* Clear the error and end-of-file flags. */
    clearerr(stdin);
}

我不知道。。。我运行了你的代码,它对我来说运行良好,所以我不知道你是如何丢失输入的。你复制了完整的代码吗

既然只使用第一个字符,为什么还要将其读入缓冲区?也许通过简化输入,您可以避免错误,只需获取字符:

...
printf("4. Merge Sort\n");
printf("5. Exit\n");
printf("\nEnter your option[1-5]: ");
fflush(stdout);  
opt1=getchar();
getchar();  // Extra "getchar" call to get rid of the newline
...
编辑:

让我们试着简化你的问题,以了解失败在哪里。请尝试运行以下代码:

int main()
{
  char input = '0';
  input = getchar();
  getchar();
  printf("%c\n", input);
  return 0;
}

在你的系统上编译/运行/工作吗?如果是这样,问题不在于如何获取字符串/char,而在于代码中的其他内容。

您正在打开“opt1”,但在switch语句中没有默认值:case。我不确定你的问题具体是什么,但我会研究一下。你应该将你的输入管理代码分成一个单独的函数,与那些处理给定输入的函数分开。使用
fflush(stidin)
是不可移植的。我认为您应该将您的输入缓冲区
input
增加到一个较大的大小,大于您期望用户能够键入的最长行数。选择一个数字,例如1024或4096。然后,这将读取整行、换行符和所有内容,您可以对其进行解析,并忽略无关字符。我尝试增加输入大小以及所有的刷新、倒带、倒带也不起作用。如果这样做,您必须通过换行符,但这远不是不可能的。@JonathanLeffler-我同意。。我相信您已经注意到了出于这个原因我额外添加的
getchar()
,但是我想,一个额外的调用来处理新行要比只查看传递的第一个char而处理OP所做的所有数组工作容易得多in@user1706853:您正在使用哪个操作系统和字符集?您是否碰巧使用了宽字符集?@user1706853-好的,试试我发布的“编辑”功能,这是否有效?如果“基础”起作用,这将有所帮助。有些时候,通过将问题分解得更小来解决问题是最容易的。我正在使用linux,我尝试了这段代码,但它仍然循环菜单,并且不接受来自stdin的任何内容