Develop a C++ program that reads a key pressed on the keyboard
and displays its code on the screen. Use the program to determine the
code for the Enter key. Then write a function named ReadOneChar ( )
that reads a character and ignores any succeeding characters until the
Enter key is pressed. The entered character should be returned by
ReadOneChar ( ).
Answers
Answered by
1
Answer:
- conio available with Windows compilers. Use the _getch() function to give you a character without waiting for the Enter key. I'm not a frequent Windows developer, but I've seen my classmates just include <conio.h> and use it. See conio.h at Wikipedia. It lists getch(), which is declared deprecated in Visual C++.
- curses available for Linux. Compatible curses implementations are available for Windows too. It has also a getch() function. (try man getch to view its manpage). See Curses at Wikipedia.
- I would recommend you to use curses if you aim for cross platform compatibility. That said, I'm sure there are functions that you can use to switch off line buffering (I believe that's called "raw mode", as opposed to "cooked mode" - look into man stty). Curses would handle that for you in a portable manner, if I'm not mistaken.
Similar questions