How to get user input without using scanf in a console application?

I am trying to allow users to simply press Enter without typing anything, and use this to indicate the default value. scanf does not do what I want, and the application still "blocks": the next line of code does not start .

The only way is to actually enter something THEN to press Enter.

I tried using NSFileHandle and fileHandleWithStandardInput; however, it now seems that the user now needs to press Ctrl-D to specify EOF.

Someone suggested using fgets, but I can’t decide what to pass as the third parameter (like FILE* ). I tried stdin, but it does not block.

How can I accept input from a user using Objective-C, and at the same time allow the user to just press Enter without having to type anything? How to read one line, even if this line is empty?

+1
source share
4 answers

Assuming that the code is not blocked, and the next line is executed immediately (as you seemed to point out at an early stage in the question, and in the comment ), you have a common problem when mixing input data other than line and line.

What happens is that there is a new line in the buffer, and fgets sees this, reads and returns, instead of doing what you really want: ignoring it, and then reading the line.

The solution is to simply ignore the part yourself and then call fgets:

 #include <stdio.h> #include <string.h> FILE* ignoreline(FILE* stream) { for (int c; (c = fgetc(stream)) != EOF;) { if (c == '\n') break; } return stream; } void example_use() { char buf[1000]; ignoreline(stdin); fgets(buf, sizeof buf, stdin); // or, since it returns the stream, can be more compact: fgets(buf, sizeof buf, ignoreline(stdin)); } int main() { // error handling omitted int n; printf("Enter a number: "); scanf("%d", &n); char buf[1000]; printf("Enter a line: "); ignoreline(stdin); // comment this line and compare the difference fgets(buf, sizeof buf, stdin); *strchr(buf, '\n') = '\0'; printf("You entered '%s'.\n", buf); return 0; } 

Note that this is also common, and it is recommended that you match the ignore with scanf (or another non-line input) to turn it into a line input. In this case, you can change it so that you can specify the difference between entering "42 abc" and "42" (in the case of "Enter number"). Some people simply use phages everywhere, and then parse this string with sscanf, and although this works, it is not necessary.

+5
source

I am using getch(); in the conio.h library, just a program is waiting for any key to be pressed

+3
source

If you use Windows, you can use the ReadConsoleInput function (see MSDN for more details):

 INPUT_RECORD keyin; DWORD r; while (ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE),&keyin,1,&r)) { if (keyin.EventType!=KEY_EVENT) continue; if (keyin.Event.KeyEvent.wVirtualKeyCode==VK_SPACE) break; ///use these VK codes to get any key input if (keyin.Event.KeyEvent.wVirtualKeyCode==VK_F1) { printf("You pressed F1\n"); } if (keyin.Event.KeyEvent.wVirtualKeyCode==VK_F2) { printf("You pressed F2\n",); } }//end while loop 

You do not need to press enter after each key. It works like a dream for me ...

+1
source

use getchar() to enter input without using the scanf ... function

+1
source

Source: https://habr.com/ru/post/1302031/


All Articles