![](/img/trans.png)
[英]How to scan for user input of both strings and ints to put into a 2D array in c
[英]how to stop scanning when input is null (scan an unknown number of ints)
我目前正在开发一个程序,该程序扫描每个int并找出其是否完美。 问题是,我不知道输入中有多少个整数,因此我想找出如何在输入结束时停止扫描。
码:
#include <stdio.h>
int main() {
int input[500], count;
for (count = 0; count < 500; count++) {
scanf("%d", &input[count]);
if (input[count] == 0)
break;
}
for (count = 0; count < 500; count++) {
if (findFactors(input[count]) % input[count] == 0)
printf("%d perfect\n", input[count]);
else if (findFactors(input[count]) % input[count] <= 2 ||
findFactors(input[count]) % input[count] >= input[count] - 2)
printf("%d almost perfect\n", input[count]);
else
printf("%d not perfect\n", input[count]);
}
}
在这种情况下,我需要输入500个数字才能运行代码。 当输入为空时,我需要它运行。 我知道有“ / 0”之类的东西,但我不知道如何在此代码中使用它。
使用函数时,请阅读文档
回报价值
这些函数返回成功匹配和分配的输入项的数量,该数量可能少于所提供的数量,在早期匹配失败的情况下甚至为零。
因此,您需要检查scanf的返回值:
if (scanf("%d", &input[count]) != 1)
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.