简体   繁体   中英

Error in job Build for constructing C++ program to determine range

I was till was experiencing some issues until i made the change to my formula for gravity. all is working now. thanks everyone. I appreciate all the great feedback

    #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;

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

This is not valid C++.

pow is a function that takes two arguments. x^y would be represented as pow(x, y) .

Also, sin(*angle) is invalid as angle is neither a pointer nor a class with a defined * operator.

I think this is what you're looking for:

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

(This is the correct formula for range)

You defined angle as a double, so you don't want to dereference it by writing *angle . pow() takes two arguments, so you probably want to write pow(velocity,2) . sin(angle)*pow(velocity,2) should work. I'd recommend sin(angle)*velocity*velocity , as you don't need to use pow(x,2) if you only want to calculate x*x .

Oh, and note that gravity is 0.0 in your code, as it is defined as velocity*9.8 while velocity is 0.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM