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

标签 c ncurses

当我创建一个文字字符串并将其添加到菜单中时,一切正常。但是如果我从用户输入一个字符串,那么菜单是“空白”。我不知道这是一个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;
}

最佳答案

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

关于c - ncurses 菜单 - 不会显示我的用户输入的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/899876/

相关文章:

c++ - NCurses – getstr() 和功能键

linux - 不同终端的不同 nCurses 行为

c - 从数组 C 中获取元素的类型

c - 为什么输出是用户的最后一个字符串?

c - 尝试读取文件时出现段错误 (C)

c - 在 C 打印垃圾中添加回车实用程序?

c - getch 和 putchar 无法正常工作且无返回

将 32 位数字转换为 16 位数字

Python Curses 用循环刷新文本

c++ - Ncurses/C/C++ : Using getstr() and preventing overflow (There must be a better way to do this)