0

I would like to setup the input stream so that each character is sent immediately to the program instead of pressing enter every time. I've tried setting stdin to unbuffered mode with setvbuf(), and used getchar(), which I know isn't an unbuffered character input function but I had trouble with getch() since there's some sort of random character appearing on the screen without me touching the keyboard. And after I choose a valid option, the program just freezes, and I only changed getchar() to getch() when that happend, nothing else. I've been trying to figure this out for a while by asking small questions that lead me to the answer but I think I should just ask for help on my end goal. So does anyone know a way to send input to a C program immediately after pressing a key that's not enter?

Matthew Schell
  • 599
  • 1
  • 6
  • 19
  • This is OS dependent. – Nate Eldredge Apr 22 '21 at 17:58
  • A simple solution to do what you want is to use specific library. If you want text program, you can use `ncurses`: https://stackoverflow.com/q/9208296/1212012 – Mathieu Apr 22 '21 at 17:59
  • 3
    Does this answer your question? [Capture characters from standard input without waiting for enter to be pressed](https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed). Short version: It can not be done portably in C++ as this is OS dependent. – Devolus Apr 22 '21 at 18:00
  • 1
    To present an answer with specificity, requires a question (problem statement) with specificity. This can be achieved by simply including a [mcve] – ryyker Apr 22 '21 at 18:23

0 Answers0