Scan multiple numbers without scanning enter C

I am trying to scan 4 numbers without pressing enter (I want it to complete the scan when it sees 4 numbers). I am using the method:

printf("Enter 4 Numbers(with space):\n");
scanf("%d %d %d %d" , &guess1 , &guess2 , &guess3 , &guess4);

but it requires spaces between numbers and pressing input at the end. So, how do I get it to get, for example: 1234 (num1 = 1, num2 = 2, etc.), and end the reception without pressing the enter key.

-1
source share
3 answers

To enter "1234"and scan as 4 digits, use "1`` to limit the width of the data entry text for each integer.

if (scanf("%1d%1d%1d%1d" , &guess1 , &guess2 , &guess3 , &guess4))!=4) Handle_Input_Error();
else Success();

stdinusually buffered. Therefore, to read data without waiting, the Entersolution is platform specific.

. scanf() ?

+1

, , , enter. . ( ), , , ​​ ncurses Linux, DOS, . , . , -, , , . , , ?

0

Just remove the spaces between% d in scanf (), and you can enter numbers like 1 2 3 4, and then press enter, the numbers will be saved in guess1, guess2, guess3 and guess4 respectively. printf ("Enter 4 numbers (with a space): \ n");

scanf("%d%d%d%d" , &guess1 , &guess2 , &guess3 , &guess4); 

so you just need to learn Technic to read numbers separated by spaces.

0
source

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


All Articles