2013-05-20 18 views
1

我一直在遇到一些問題,直到我改變我的重力公式。現在都在工作。感謝大家。我感謝所有偉大的反饋工作中的錯誤構建C++程序來確定範圍

#include <iostream> 
    #include <string> 
    #include <cstdlib> 
    #include <cmath> 

    using namespace std; 

    int main() 
    { 
     // Initialize objects: 

      double angle = 0.0; 
     double velocity = 0.0; 
     double range = 0.0; 
     const double PI = 3.141592653589793238; 
     const double gravity = 9.8; //meters pers second 

     // Input: 

     cout << "takeoff angle: "; 
     cin >> angle; 
     cout << "Please enter velocity: "; 
     cin >> velocity; 

     // Process 

     angle = angle * PI/180; 
     range = sin(2 * angle) * velocity * velocity/gravity; 

     cout << " range " << range << endl; 
     system("pause"); 

     return 0; 

} 

回答

0

您定義angle爲double,所以你不寫*angle要取消對它的引用。 pow()需要兩個參數,所以你可能要寫pow(velocity,2)sin(angle)*pow(velocity,2)應該工作。我建議您使用sin(angle)*velocity*velocity,因爲如果您只想計算x*x,則無需使用pow(x,2)

哦,並且請注意,代碼中的引力爲0.0,因爲它在速度爲0.0時定義爲velocity*9.8

+0

這可能就是即時得到結果上面列出的,是吧?我正在努力解決它.. –

2
range = sin(* angle)*velocity pow(2); 

這是無效的C++。

pow是一個帶有兩個參數的函數。 x^y將被表示爲pow(x, y)

此外,sin(*angle)是無效的,因爲angle既不是一個指針,也不具有限定*操作者的類。

我認爲這是你在找什麼:

range = sin(2 * angle) * velocity * velocity/gravity; 
// (No need to use pow(velocity, 2) over velocity * velocity) 

(這是一系列正確的公式)

+0

感謝您的意見。固定公式,現在我得到「請輸入角度:2 請輸入速度:5 範圍1.#INF 按任意鍵繼續......」 –

+0

將重力值改爲9.8。乘以恰好爲'0'的速度將導致除以零,這導致Infinity值彈出。 –