c structure: Input requires number line by line by hitting "enter" after each number

102 views Asked by At

How can I add the 10 digit number without write one digit in each line.

struct test {
  unsigned short telephone[10];
  //Some Code Here ... 
}

for (j = 0; j < 10; j++) {
  scanf("%d", & seats[position - 1].telephone[j]);
}
1

There are 1 answers

0
Sourav Ghosh On

Two things to mention,

  • Honor the data type
  • Make use of the maximum field width with scanf.

Write

 scanf("%1hu", &seats[position-1].telephone[j]);
     //  ^

which reads only 1 element from the input .

Note: the h is a length modifiers , which is described as

h

Specifies that a following d, i, o, u, x, X , or n conversion specifier applies to an argument with type pointer to short int or unsigned short int.

Also related, from C11, chapter ยง7.21.6.2/p9, (emphasis mine)

An input item is defined as the longest sequence of input characters which does not exceed any specified field width and which is, or is a prefix of, a matching input sequence.