0

Basically I want to input a pass code composed of 4 digits from the terminal. I have some sloppy solutions but I want something clean. I want the user to write a digit and I'll put it in an array without having to press "enter". And then the user will give the second digit and I'll put it in the array... So for example it will give some thing like this:

>1      the array [1, 0, 0, 0]
>12     the array [1, 2, 0, 0]
>123    the array [1, 2, 3, 0]
>1234   the array [1, 2, 3, 4]
>the rest of the program
the busybee
  • 10,755
  • 3
  • 13
  • 30
  • 1
    Depends on the operating system. Which operating system are you using? – user3386109 Oct 14 '19 at 22:36
  • One easy way is `ncurses`. – Steve Summit Oct 14 '19 at 22:39
  • 1
    MSVC has non-standard library functions [`_getch()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017) and [`_getche()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getche-getwche?view=vs-2017). I have always been mystifed by the lack of a single-key entry in standard C. In this case though, the input cannot be redirected as it is not through `stdin`. – Weather Vane Oct 14 '19 at 22:39
  • What platform are you on? There isn't a single soultion for all platforms. Input from the terminal is usually line buffered, and disabling line buffering can only be done in a platform specific manner. – Ankush Oct 14 '19 at 22:40
  • @user3386109 @ Ankush I use Windows but i care about knowing how on Unix based systems also – Abdelraim Chernai Oct 14 '19 at 22:49
  • MSVC also has the non-blocking [`_kbhit()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/kbhit?view=vs-2017) which is useful to check when you don't want to commit to the blocking functions I mentioned. – Weather Vane Oct 14 '19 at 22:52
  • @SteveSummit ncurses looks great, but i am on a project and i can't just restart it using it but thanks tho ! – Abdelraim Chernai Oct 14 '19 at 23:06
  • 1
    On unix systems, you can [set the terminal into non-canonical mode](https://stackoverflow.com/questions/29723205/). – user3386109 Oct 14 '19 at 23:51
  • [C FAQ list answer](http://c-faq.com/osdep/cbreak.html). (Somewhat dated, though.) – Steve Summit Oct 15 '19 at 11:53

0 Answers0