[英]Issue with simple while loop in C terminating early
I have the following code: 我有以下代码:
//get distances
int a = -1;
int b = -1;
while ((a != 0) && (b != 0)) {
scanf("%d %d", &a, &b);
printf("The shortest path from %d to %d is %.1lf meters.\n", a, b, D[a][b]);
}
The loop seems to terminate after one input, despite the fact that a and b are not inputed as 0. 尽管a和b未输入为0,但循环似乎在一个输入后终止。
ie: 即:
0 2
The shortest path from 0 to 2 is 237.7 meters.
Not really sure why it's doing that so any help would be appreciated. 不太确定为什么要这样做,因此不胜感激。
Then it terminates 然后终止
Full code in case needed 完整代码,以备不时之需
#include <stdio.h>
#include <stdlib.h>
#define INF 41e6
double** new2Ddouble(int n, int m);
void free2D(double **a);
int main() {
//get # of nodes
int size = 0;
scanf("%d", &size);
//create matrix
double **D = new2Ddouble(size, size);
//fill with inf for D[i][j]
int i;
int j;
for(i=0; i<size; i++) {
for(j=0; j<size;j++){
D[i][j] = INF;
}
}
//fill D[i][i] with INF
for(i=0;i<size;i++) D[i][i] = INF;
int exit = 0;
int I;
int J;
double d;
while(exit != 1) {
//populate values in matrix
scanf("%d %d %lf", &I, &J, &d);
if(I == 0 && J == 0 && d == 0){
//we can exit
exit = 1;
} else {
D[I][J] = d;
}
}
//calculate distances
/* Floyd-Warshall Algorithm */
int k;
for (k=0; k<size; ++k)
for (i=0; i<size; ++i)
for (j=0; j<size; ++j)
if (D[i][k]+D[k][j] < D[i][j])
D[i][j] = D[i][k]+D[k][j];
exit = 0;
//get distances
int a = -1;
int b = -1;
while ((a != 0) && (b != 0)) {
scanf("%d %d", &a, &b);
printf("The shortest path from %d to %d is %.1lf meters.\n", a, b, D[a][b]);
}
return 0;
}
double** new2Ddouble(int n, int m) {
int i;
double **ret = (double**) malloc(n*sizeof(double*));
double *a = (double*) malloc(n*m*sizeof(double));
for (i=0; i<n; ++i) ret[i] = &a[i*m];
return ret;
}
void free2D(double **a) { free(a[0]); free(a); }
scanf("%d %d", &a, &b);
In this line you are scanning values for a
and b
0 and 2 respectively. 在此行中,您分别扫描a
和b
值0和2。 So once a new value is scanned to these variables then your while condition fails because as you have showed a
is 0
and the second condition is never checked because 0 && (0|1) = 0
因此,一旦将新值扫描到这些变量,则您的while条件就会失败,因为您显示a
为0
,而从不检查第二个条件,因为0 && (0|1) = 0
Once a=0
the condition fails and exits the loop. 一旦a=0
,条件将失败并退出循环。
while ((a != 0) && (b != 0))
This loop will terminate when a
or b
is zero and will continue when a
and b
are not zero. 当a
或 b
为零时,此循环将终止,而当a
和 b
不为零时,此循环将继续。 You enter a
as 0 and as the condition a!=0
fails,your loop terminates. 输入a
为0,并且当条件a!=0
失败时,循环终止。
there are a couple of problems with this line:
scanf("%d %d", &a, &b);
Note: scanf does not automatically consume white space,
so on the second iteration
the next input char is a newline.
so scanf fails
Note: all I/O statements
should have the returned value checked
to assure success of the operation
to fix those items, write it like so:
if( 2 != scanf(" %d %d", &a, &b) )
{
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
the leading ' ' in the format string causes leading
white space, like the newline, to be consumed
the 'if' checks the returned value from
scanf to assure that all the input conversions were successful
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.