0

当我创建一个文字字符串并将其添加到菜单时,一切正常。但是如果我从用户那里输入一个字符串,那么菜单就是“空白”。我不知道这是一个诅咒/菜单问题,还是一个 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 回答