ncurses 菜单 - 不会显示我的用户输入的字符串

发布于 2024-07-21 05:51:05 字数 881 浏览 4 评论 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;
}

When I create a literal string and add it to the menu, everything works fine. But if I input a string from the user, then the menu is "blank". I don't know if this is a curses/menu problem, or a C problem, as I am a beginner at both.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2024-07-28 05:51:05

问题是输入字符串末尾的“\n”。 删除它将使这项工作正常进行。

The problem was the '\n' at the end of the inputted string. Removing that will make this work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文