2009-05-22 13 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'でした。これを削除するとこの作業が行われます。

関連する問題