简体   繁体   English

fgets在C中不起作用(stdin看起来已满)

[英]fgets doesn't work (stdin looks full) in C

This is my code, please have a look. 这是我的代码,请看看。 My menu keeps looping and does not ask for any input. 我的菜单不断循环播放,不要求任何输入。 I do not know why fgets does not work as expected. 我不知道为什么fgets不能按预期工作。

When I run my code it will keep looping even though when I get rid of the loop: 当我运行我的代码时,即使我摆脱了循环,它也会继续循环:

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);
}

I don't know... I ran your code and it worked fine for me, so I don't know how you're missing the input. 我不知道...我运行了您的代码,它对我来说很好用,所以我不知道您是怎么缺少输入的。 Did you copy the full code? 您复制了完整代码吗?

Since you're only using the first character why read it into a buffer anyway? 由于您只使用第一个字符,为什么还要将其读入缓冲区? Perhaps with simplified input you can avoid your error, just get the char: 也许使用简化的输入可以避免出现错误,只需获取char:

...
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
...

EDIT: 编辑:

Let's try simplifying your problem to understand where the failure is. 让我们尝试简化您的问题,以了解故障所在。 Try running this code instead: 尝试运行以下代码:

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

Does that compile/run/work on your system? 是否可以在您的系统上编译/运行/工作? If so the problem isn't with how you're getting the string/char but something else with your code. 如果是这样,问题不在于您如何获取字符串/字符,而与代码有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM