0

リテラル文字列を作成してメニューに追加すると、すべて正常に動作します。しかし、ユーザーから文字列を入力すると、メニューは「空白」になります。私はどちらも初心者なので、これが curses/menu の問題なのか C の問題なのかわかりません。

#include <curses.h>
#include <menu.h>
#include <malloc.h>

int main()
{
    MENU *my_menu;
    ITEM **my_items;
    char c;

// works
    char my_string[20] = "this is the string";

// user-inputted string, comment these 2 lines out to make this program work
    printf("enter something: ");
    fgets(my_string, 19, stdin);

    initscr();
    noecho();
    crmode();

    my_items = (ITEM **)calloc(2, sizeof(ITEM *));
    my_items[0] = new_item(my_string, my_string);
    my_items[1] = (ITEM *)NULL;
    my_menu = new_menu(my_items);

    post_menu(my_menu);
    refresh();

    while ((c = getch()) != 'q') { }

    free_item(my_items[0]);
    free_item(my_items[1]);
    free_menu(my_menu);

    endwin();

    return 0;
}
4

1 に答える 1

1

問題は、入力文字列の末尾にある「\n」でした。それを削除すると、この作業が行われます。

于 2009-05-23T19:26:27.383 に答える