0

これは私のコードです。ご覧ください。私のメニューはループし続け、入力を求めません。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);
}
4

1 に答える 1

1

わかりません...私はあなたのコードを実行しましたが、私にとってはうまくいきました。コード全体をコピーしましたか?

最初の文字しか使用していないのに、なぜそれをバッファに読み込むのでしょうか? おそらく単純化された入力を使用すると、エラーを回避できます。文字を取得するだけです。

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

それはあなたのシステムでコンパイル/実行/動作しますか? その場合、問題は文字列/文字を取得する方法ではなく、コードに何か他のものがあります。

于 2012-09-28T17:05:27.103 に答える