数组中使用scanf的整数输入

时间:2021-04-10 16:09:29

I am taking multiple integer inputs using scanf and saving it in an array

我使用scanf接收多个整数输入并将其保存到一个数组中

while(scanf("%d",&array[i++])==1);

the input integers are separated by white spaces for example

例如,输入整数由空格分隔

12 345 132 123

I read this solution in another post.

我在另一篇文章中读到了这个解决方案。

But the problem is the while loop is not terminating.

但是问题是while循环没有终止。

Whats the problem with the statement?

这个声明有什么问题吗?

4 个解决方案

#1


9  

OP is using the Enter or '\n' to indicate the end of input and spaces as number delimiters. scanf("%d",... does not distinguish between these white-spaces. In OP's while() loop, scanf() consumes the '\n' waiting for additional input.

OP使用Enter或'\n'表示输入的结束和空格作为数字分隔符。scanf(“% d”,…不区分这些空白。在OP的while()循环中,scanf()使用等待额外输入的'\n'。

Instead, read a line with fgets() and then use sscanf(), strtol(), etc. to process it. (strtol() is best, but OP is using scanf() family)

相反,使用fgets()读取一行,然后使用sscanf()、strtol()等进行处理。(strtol()是最好的,但是OP使用scanf()家族)

char buf[100];
if (fgets(buf, sizeof buf, stdin) != NULL) {
  char *p = buf;
  int n;
  while (sscanf(p, "%d %n", &array[i], &n) == 1) {
     ; // do something with array[i]
     i++;  // Increment after success @BLUEPIXY
     p += n;
  }
  if (*p != '\0') HandleLeftOverNonNumericInput();
}

#2


4  

//Better do it in this way
int main()
{
  int number,array[20],i=0;
  scanf("%d",&number);//Number of scanfs
  while(i<number)
  scanf("%d",&array[i++]);
  return 0;
}

#3


1  

You should try to write your statement like this:

你应该试着这样写你的陈述:

while ( ( scanf("%d",&array[i++] ) != -1 ) && ( i < n ) ) { ... }

Please note the boundary check.

请注意边界检查。

As people keep saying, scanf is not your friend when parsing real input from normal humans. There are many pitfalls in its handling of error cases.

正如人们常说的,scanf在解析来自普通人的真实输入时不是你的朋友。它在处理错误案例时存在许多缺陷。

See also:

参见:

#4


0  

There is nothing wrong with your code as it stands. And as long as the number of integers entered does not exceed the size of array, the program runs until EOF is entered. i.e. the following works:

目前的代码没有任何问题。只要输入的整数数量不超过数组的大小,程序就会一直运行到输入EOF为止。例如以下工作:

int main(void)
{
    int array[20] = {0};
    int i=0;
    while(scanf("%d", &array[i++]) == 1);
    return 0;   
}  

As BLUEPIXY says, you must enter the correct keystroke for EOF.

正如BLUEPIXY所说,你必须输入正确的EOF按键。

#1


9  

OP is using the Enter or '\n' to indicate the end of input and spaces as number delimiters. scanf("%d",... does not distinguish between these white-spaces. In OP's while() loop, scanf() consumes the '\n' waiting for additional input.

OP使用Enter或'\n'表示输入的结束和空格作为数字分隔符。scanf(“% d”,…不区分这些空白。在OP的while()循环中,scanf()使用等待额外输入的'\n'。

Instead, read a line with fgets() and then use sscanf(), strtol(), etc. to process it. (strtol() is best, but OP is using scanf() family)

相反,使用fgets()读取一行,然后使用sscanf()、strtol()等进行处理。(strtol()是最好的,但是OP使用scanf()家族)

char buf[100];
if (fgets(buf, sizeof buf, stdin) != NULL) {
  char *p = buf;
  int n;
  while (sscanf(p, "%d %n", &array[i], &n) == 1) {
     ; // do something with array[i]
     i++;  // Increment after success @BLUEPIXY
     p += n;
  }
  if (*p != '\0') HandleLeftOverNonNumericInput();
}

#2


4  

//Better do it in this way
int main()
{
  int number,array[20],i=0;
  scanf("%d",&number);//Number of scanfs
  while(i<number)
  scanf("%d",&array[i++]);
  return 0;
}

#3


1  

You should try to write your statement like this:

你应该试着这样写你的陈述:

while ( ( scanf("%d",&array[i++] ) != -1 ) && ( i < n ) ) { ... }

Please note the boundary check.

请注意边界检查。

As people keep saying, scanf is not your friend when parsing real input from normal humans. There are many pitfalls in its handling of error cases.

正如人们常说的,scanf在解析来自普通人的真实输入时不是你的朋友。它在处理错误案例时存在许多缺陷。

See also:

参见:

#4


0  

There is nothing wrong with your code as it stands. And as long as the number of integers entered does not exceed the size of array, the program runs until EOF is entered. i.e. the following works:

目前的代码没有任何问题。只要输入的整数数量不超过数组的大小,程序就会一直运行到输入EOF为止。例如以下工作:

int main(void)
{
    int array[20] = {0};
    int i=0;
    while(scanf("%d", &array[i++]) == 1);
    return 0;   
}  

As BLUEPIXY says, you must enter the correct keystroke for EOF.

正如BLUEPIXY所说,你必须输入正确的EOF按键。