最佳方式(Y / N)

时间:2015-05-13 03:37:05

标签: c menu do-while ncurses

我写这段代码:

#include <stdio.h>
#include <curses.h>
void salir (void);
int main(int argc, char** argv){
    char h;
    initscr();
    start_color();
    init_pair(1,COLOR_BLACK,COLOR_BLUE); 
    init_pair(3,COLOR_BLACK,COLOR_WHITE);  
    bkgd(COLOR_PAIR(1));
    attron(COLOR_PAIR(3));
    move(2,1);
    printw("Welcome to my first ncurses program :D \n");
    move(3,1);
    printw("Would you like to read the manual? (Y/N)\n");
    do{
        h = getch();
        if(h == 'y' || h == 'Y'){
            printw("1- You must enter the numbers of rows and columns you want\n2- Then you have to specificate the type of data to enter\n");  
            break;
        }

        else if (!(h == 'n' || h == 'N')){ 
            printw("Enter a valid option\n");
        }
    } while (!(h == 'n' || h=='N'));

    printw("hola");
    attroff(COLOR_PAIR(3));
    refresh();
    getch();
    salir();

}

void salir(){
    endwin();
    exit(0);
}

我怀疑Y / N菜单。做这个的最好方式是什么?另外,我不想在窗口中显示输入的字符。这是丑陋和不必要的。

1 个答案:

答案 0 :(得分:2)

我倾向于使用一个键激活您的菜单,而任何其他键都会继续,而不是只接受“Y&#39;或者&#39; N&#39;换句话说,执行&#34;点击进入菜单,任何其他键继续&#34;之类的事情。在代码中,这可能看起来像:

printw("Hit 'H' for help, any other key to continue\n");
h = getch();
if(h == 'y' || h == 'Y') {
    printw("Helpful stuff here.\n");
}

printw("Getting on with our lives here, with or without help);

如果您不想显示输入的字符,可以在ncurses中切换回显:

echo()    // Turns on echo of keystrokes
noecho()  // Turns off echo of keystrokes

有关这些功能的详细信息here