【发布时间】:2020-04-21 05:03:13
【问题描述】:
我的代码有什么问题? 当给出以下输入(通过文件)时:
6.02
110 223 144 208 199.5 890
200 69.5 300 138.7 190 601
它打印ERROR: invalid price in airline # 1,但它不应该这样做。
这是我的代码。
int fillPricesTable(double flightsPrices[][DEST],int n,double* dollarRate)
{
//n is the number of rows
double Price;
int AirLinesCounter=0;
while (scanf("%lf",dollarRate)==EOF || *dollarRate<=0)
{
errorDollar();
fflush(stdin);
}
for (int i=0;i<n;++i)
{
for (int j=0;j<6;++j)
{
if (scanf("%lf",&Price)==EOF || Price<=0)
{
printf("ERROR: invalid price in airline # %d\n", i);
return -1;
}
flightsPrices[i][j]=Price;
}
AirLinesCounter++;
if(scanf("%lf",&Price)==EOF)
break;
}
return AirLinesCounter;
}
【问题讨论】:
-
n是什么?请创建一个minimal, reproducible example 并用它更新您的问题。 -
注意Using
fflush(stdin)的语义——它至少是不可移植的。 -
在标准输入上刷新未定义
-
不是您要问的实际问题,而是一般提示:
scanf也可以返回 0。查看文档以了解这意味着什么。考虑一下您的代码在scanf确实返回 0 的情况下会做什么。 -
@hyde 按照建议更新了我的代码,但仍然是相同的错误
标签: c scanf c99 eof io-redirection