2013-01-23 106 views
1

当我编译我的程序它似乎没有执行我的公式我不能图什么我做错了帮助,将不胜感激C++ sqrt函数

int main() 
    { 
int distance, Xvalue, Yvalue; 
double x1,y1,x2,y2; 

cout << "\n Please enter X1 value: "; 
cin >> x1; 
cout << " Please enter X2 value: "; 
cin >> x2; 
cout << "\n Please enter Y1 value: "; 
cin >> y1; 
cout << " Please enter Y2 value: "; 
cin >> y2; 
    Xvalue = (x1 - x2); 
    Yvalue = (y1 - y2); 
distance = sqrt(Xvalue * Xvalue + Yvalue * Yvalue); 

cout << "This is the distance between the two points" <<distance<< 


    cout << endl << endl; 
    system ("pause"); 
    return 0; 
} 
+0

执行它时会发生什么? –

+7

对于初学者来说,你的配方是错误的。 – chris

+2

其余的代码在哪里?那甚至不会编译 – alrikai

回答

1

我敢肯定,这可能是你的问题的一部分:

Xvalue = (x1 - x2); 
Yvalue = (y1 - y1); 

大概应该是:

Xvalue = (x1 - x2); 
Yvalue = (y1 - y2); 
+0

-_-我没有注意到,但它仍然没有sqrt我的回答 – user2002228

1

双变量的差异可以是double和您的Yvalue始终计算为zero

其实,你的配方本身是错误的。

Distance Formula: Given the two points (x1, y1) and (x2, y2), 

这些点之间的距离由下式给出:

d = sqrt((x2-x1)^2 + (y2-y1)^2) 

注意,u的减去而不是加上的差的平方。

double x1,y1,x2,y2,distance, Xvalue, Yvalue; 
Xvalue = (x1 - x2); 
Yvalue = (y1 - y2); 
distance = sqrt(Xvalue * Xvalue + Yvalue * Yvalue); 
+0

它没有编译当我尝试这种方式= \ – user2002228

+0

我的更改不会影响编译你的代码。事实上,我刚刚提到了小的更正。 –

2

变化的距离,x值和y值双打

0

第一COUT输入值,所以你可以肯定,如果这个问题是不是在输入

cout<<x1<<endl; 
cout<<x2<<endl; 
cout<<y1<<endl; 
cout<<y2<<endl; 

,那么你正在试图清点。 .. cout!

cout<<"this is the"<< distance << cout ... // cout again, is not very good! 

尝试

cout<< "this is the"<< distance <<endl; 
cout << endl << endl; 

反正..除非你需要那些 “INT” 的具体原因,最好是有双打。 (你仍然可以用“floor(value)”稍后将它们舍入)