2009-05-22 2 views
0

리터럴 문자열을 만들어 메뉴에 추가하면 모든 것이 올바르게 작동합니다. 그러나 사용자로부터 문자열을 입력하면 메뉴는 "비어 있습니다". 나는 curses/menu 문제인지 C 문제인지, 둘 다 초보자인지 모르겠다.ncurses 메뉴 - 내 사용자 입력 문자열을 표시하지 않습니다.

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

fgets 호출을 으로 변경하여이 기능을 사용할 수 있습니다. scanf ("% s", my_string); 대신 차이점이 무엇인지 모릅니다. 나는 이것이 "해결 된 것"이라고 생각할 것이다. – Kirkland

답변

1

입력 한 문자열의 끝에 '\ n'문제가있었습니다. 이를 제거하면이 작업이 가능합니다.

관련 문제