I'm writing code where I need to get input without the user pressing ENTER. The code is basically that I need to get 4 numbers from 1-6 without the user pressing ENTER all in one line.
I tried doing so with getchar(), but it doesn't work.
How do I do that?
Here's that part of the code:
int p1 = 0;
int p2 = 0;
int p3 = 0;
int p4 = 0;
p1 = getchar() - 48;
p2 = getchar() - 48;
p3 = getchar() - 48;
p4 = getchar() - 48;
This is a C FAQ: 12.5 How can I read one character at a time, without waiting for the RETURN key?
To quote:
Depending on which operating system you're using and what libraries you have available, you may be able to use one (or more!) of the following techniques:
There is no standard function for this.
There are some custom solutions e.g. https://www.cs.uleth.ca/~holzmann/C/system/ttyraw.c But your mileage may vary.
However if this kind of dynamic use of the terminal is core to your program I would recommend using ncurses or something similar.