Consider this code:
if(initscr() == NULL) ERROR("Cannot start ncurses mode.\n");
keypad(stdscr, TRUE);
cbreak();
int reply = getch();
if(reply == 'y')
printw("yes!\n");
else if(reply == 'n')
printw("no!\n");
else
printw("invalid answer!\n");
refresh();
endwin();
Independent of key that I type, the program closes without print any message.
Can someone explain the behavior of this program? Thanks in advance.
You call
printw()to print one of three messages, thenrefresh()to cause the message to be displayed. So far, so good.You then immediately call
endwin(), which (depending on your termcap/terminfo settings) will likely clear the screen.Chances are the message is actually displayed; it just doesn’t stay on the screen long enough for you to read it.
Try adding a delay or another
getch()call after therefresh()call.