我的程序中输入错误有问题.它从坐标中读取坐标并计算最大长度.它应该读到EOF.正确的坐标格式是例如[5,10].问题是,当我写"asdf"时.然后我得到4x不正确的输入,我只想要它一次.有人能帮助我吗?先感谢您.
while ((c1=(scanf(" %c %lf %c %lf %c", &f, &cx, &g, &cy, &h))) != EOF){
if (c1 != 5 || h != ']' || g != ',' || f != '['){
printf("Incorrect input. \n");
continue;
}
for (int i = 0; i < pocet; i++) {
u = polex[i] - cx;
v = poley[i] - cy;
result = sqrt((u*u) + (v*v));
if (result>max){
max = result;
}
}
printf("Max: %g\n", max);
max = 0;
}
Run Code Online (Sandbox Code Playgroud)
如果scanf没有读取所有格式(即它返回的情况少于5你的情况)那么你将失去同步,因为下一个调用scanf将尝试从最后一个停止的地方读取.
我建议你fgets先用它来读取整行,然后sscanf用来解析输入.就是这样的
while (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
c1 = sscanf(buffer, ...);
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36 次 |
| 最近记录: |