求两点之间的坐标。代码在下面,显示错误结果也在下面,求原因。

img

img


#include
#include

img

因为第一个scanf()从输入获取坐标值后,会把输入的换行符留在缓冲区,从而让第二个scanf()读取失败,所以计算出的结果就是错的,所以可以在第一个scanf()语句后,加个getchar()读走这个换行符;

然后根据样例提示,打印提示信息时需要加个换行符。

修改如下:

参考链接:

#include <stdio.h>
#include <math.h>

int main(void){
    
    double x1,y1,x2,y2;
    printf("Please input the coordinates of two points:\n");
    
    printf("point 1(x,y):\n");
    // https://blog.csdn.net/Wuyeyu2001/article/details/127299540
    scanf("(%lf,%lf)",&x1,&y1);
//    printf("x1=%f,y1=%f\n",x1,y1);
    
    getchar();  //把scanf()函数读取坐标值后丢弃在缓冲区的换行符读走 
    
    printf("point 2(x,y):\n");
    scanf("(%lf,%lf)",&x2,&y2);
//    printf("x2=%f,y2=%f\n",x2,y2);
    
    double dis = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    printf("This distance is: %.4f",dis);
    
    return 0;
}


img

看不到输入啊