2013-01-23 105 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)」稍後將它們舍入)