我只是想比较两个整数。代码运行正常然后给出错误的结果。
int userenter;
int compareseatfirst(Flights *FDA[], String destination, String dep_date, int FC_seats)
{
int j=0;
for (int i=0;i<MAXARRAYSIZE;i++)
{
//test for null
if (FDA[i]==NULL)
{
i++;//skip over this flight record
}
else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FDA[i]->FC_seats<userenter))
{
printf("NOT ENOUGH");
getchar();
j++;
if(j >= 10)
{
break; // Stop Looping as Array is full
}
}
else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FDA[i]->FC_seats==userenter))
{
printf("JUST ENOUGH");
j++;
if(j >= 10)
{
break; // Stop Looping as Array is full
}
}
else if(strcmp(FDA[i]->dep_date,dep_date)==0 && (strcmp(FDA[i]->destination,destination)==0) && (FDA[i]->FC_seats>userenter))
{
printf("ENOUGH");
j++;
if(j >= 10)
{
break; // Stop Looping as Array is full
}
}
}
return j; // Return Count of the Flights found.
}
当它运行时,尽管用户输入的数量大于或等于FC_seats,但它都会给出“足够”的答案。
答案 0 :(得分:0)
跟踪循环中的变量i
,您就会明白了。逻辑不应该是:哦,我跳过这条记录,所以我增加i
。这是错误的,因为i
无论如何都会增加。逻辑应该是:哦,我跳过这个条目,所以我在这个循环中什么都不做。 - M Oehm