From man getch
:
Note that some keys may be the same as commonly used control keys, e.g., KEY_ENTER versus control/M, KEY_BACKSPACE versus control/H. Some curses implementations may differ according to whether they treat these control keys specially (and ignore the terminfo), or use the terminfo definitions. Ncurses uses the terminfo definition. If it says that KEY_ENTER is control/M, getch will return KEY_ENTER when you press con- trol/M.
That means getch()
returns KEY_ENTER
instead of '\n'
when it reads the enter key, and thus your while
loop never terminates.