0

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]);
}
tremendows
  • 4,262
  • 3
  • 34
  • 51
arvins
  • 19
  • 5

1 Answers1

1

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.

Michał Walenciak
  • 4,257
  • 4
  • 33
  • 61
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261