在我输入一个字母并按回车键之前,printf 不会给出输出
The printf doesn't give output until I type a letter and press enter
#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:%f\n", dis);
return 0;
}
我写这段代码是为了给出两点之间的距离,但是当我 运行 它时,它不会给出最后一个 printf
直到我输入任何字母并按回车键。
正如评论中已经指出的那样,在您的代码的第 9 行 (scanf("%f %f ",&g1,&g2);
)
在第二个“%f
”之后有一个额外的space,所以scanf()期望在一个浮点字符之后读取一个白色的space。
替换为
scanf("%f %f",&g1,&g2);
修复它。
#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:%f\n", dis);
return 0;
}
我写这段代码是为了给出两点之间的距离,但是当我 运行 它时,它不会给出最后一个 printf
直到我输入任何字母并按回车键。
正如评论中已经指出的那样,在您的代码的第 9 行 (scanf("%f %f ",&g1,&g2);
)
在第二个“%f
”之后有一个额外的space,所以scanf()期望在一个浮点字符之后读取一个白色的space。
替换为
scanf("%f %f",&g1,&g2);
修复它。