在我输入一个字母并按回车键之前,printf不会给出输出
#include <stdio.h>
#include <math.h>
int main() {
float g1, g2, c1, c2, dismul, cadd, csub, dis;
printf("Enter the latitudes of the places (L1 and L2): ");
scanf("%f %f", &c1 ,&c2);
printf("Enter the longitudes of the places (G1 and G2): ");
scanf("%f %f ", &g1, &g2);
cadd = cos(c1 + c2);
csub = cos(g2-g1);
dismul = cadd * csub;
dis = 3963 * acos(dismul);
printf("The distance between the places is:%fn", dis);
return 0;
}
我写了这段代码来给出两点之间的距离,但是当我运行它时,它不会给出最后一个,printf直到我输入任何字母并按回车键。
回答
正如评论中已经指出的那样,在代码的第 9 行 ( scanf("%f %f ",&g1,&g2);)
第二个“ %f”之后有一个额外的空格,因此 scanf() 期望在浮点字符之后读取空格。
将其替换为
scanf("%f %f",&g1,&g2);
要解决这个问题。