我在ncurses中遇到菜單問題。我試圖建立一個菜單,讓用戶選擇一個選項,並根據他們的選擇設置一個名爲num_players
的int。 我這樣做boost::lexical_cast
和item_name(current_item(my_menu))
但我每次打電話current_item(my_menu)
我只是得到NULL
。 這裏是有問題的代碼示例:ncurses,menu.h和current_item()的問題
char *choices[] = {"1", "2", "3", "4", "5", "6"};
//create the dynamic array for the items and their description
ITEM** my_items;
MENU *my_menu;
int num_choices = 6;
my_items = new ITEM*;
for (int x = 0; x < num_choices; x++)
{
my_items[x] = new_item(choices[x], choices[x]);
}
my_items[6] = (ITEM*)NULL;
my_menu = new_menu((ITEM**)my_items);
set_menu_mark(my_menu, " * ");
set_current_item(my_menu, my_items[0]);
post_menu(my_menu);
wrefresh(scr);
int c;
while((c = wgetch(scr)) != '\n')
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
}
//right here, calling current_item just gives me null
//am I supposed to unpost the menu first?
//what am I doing wrong? this is frustrating
ITEM* cur = current_item(my_menu);
setNumPlayers((char*) item_name(cur));
unpost_menu(my_menu);
free_item(my_items[0]);
free_item(my_items[1]);
free_item(my_items[2]);
//etc etc
非常感謝,我覺得很愚蠢。 – destrovel