C ++将东西转移到一个不起作用的函数中

时间:2015-11-15 01:37:53

标签: c++ function

我正在开展一个学校项目,在这个项目中,我应该采用各种“FIXME”部分,并将它们提供给主要功能之外的功能。我以为我把一切都搞定了,然后什么都没有用。我重新开始,我把它缩小到导致问题的功能。我将在下面的代码中标记它。也就是说,我可以轻松地将其格式化为函数,并且我的函数语法是正确的答案,但是如果我在函数中有东西而不是函数则完全不同。这是没有函数的代码:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)

                     // Given time, angle, velocity, and gravity
                     // Update x and y values
void Trajectory(double t, double a, double v,
    double& x, double& y) {
    x = v * t * cos(a);
    y = v * t * sin(a) - 0.5 * grav * t * t;
    return;
}

// convert degree value to radians
double DegToRad(double deg) {
    return ((deg * pi) / 180.0);
}

// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
    cout << "Time " << fixed << setprecision(0)
        << setw(3) << t << "   x = " << setw(3)
        << x << "   y = " << setw(3) << y << endl;
    return;
}

void PrintIntro() { //This function is going to print the intro to the game!
    cout << "Welcome to Upset Fowl!\n";
    cout << "The objective is to hit the Mean Swine by launching an Upset    Fowl.\n";
}

int main() {
    double t = 1.0; // time (s)
    double fowlY = 0.0; // object's height above ground (m)
    double fowlAngle = 0.0; // angle of launch of fowl (rad)
    double fowlVel = 0.0; // velocity of fowl (m/s)
    double fowlX = 0.0; // object's horiz. dist. from start (m)
    double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)
    double swineX = 0.0; // distance to swine (m)
    double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
    bool didHitSwine = false; // did hit the swine?

    srand(time(0));
    swineX = 50; //(rand() % 201) + 50; I took out the randomness so I can keep track of answers easily.

    PrintIntro();

    cout << "\nThe Mean Swine is " << swineX << " meters away.\n";
    cout << "Enter fowl launch angle (deg): ";
    cin >> fowlAngle;
    fowlAngle = ((fowlAngle * pi) / 180.0); // convert to radians
    cout << "Enter fowl launch velocity (m/s): ";
    cin >> fowlVel;


    // FIXME Make into a function called LaunchFaowl
    do {
        PrintUpdate(t, fowlX, fowlY);
        Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
        t = t + 1.0;
    } while (fowlY > 0.0); // while above ground
    PrintUpdate(t, fowlX, fowlY);


    fowlLandingX = fowlX;


    // FIXME Make into a function called DtrmnIfHit
    beforeSwineX = swineX - 30;
    if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
        cout << "Hit'em!!!" << endl;
        didHitSwine = true;
    }
    else {
        cout << "Missed'em..." << endl;
        didHitSwine = false;
    }

    return 0;
}

以下是具有以下功能的代码:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)

                         // Given time, angle, velocity, and gravity
                         // Update x and y values
void Trajectory(double t, double a, double v,
    double& x, double& y) {
    x = v * t * cos(a);
    y = v * t * sin(a) - 0.5 * grav * t * t;
    return;
}

// convert degree value to radians
double DegToRad(double deg) {
    return ((deg * pi) / 180.0);
}

// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
    cout << "Time " << fixed << setprecision(0)
        << setw(3) << t << "   x = " << setw(3)
        << x << "   y = " << setw(3) << y << endl;
    return;
}

void PrintIntro() { //This function is going to print the intro to the game!
    cout << "Welcome to Upset Fowl!\n";
    cout << "The objective is to hit the Mean Swine by launching an Upset Fowl.\n";
}

void GetUsrInpt(double piggy, double slope, double Velocity) { // FIXME Make into a function called GetUsrInpt
    cout << "\nThe Mean Swine is " << piggy << " meters away.\n";
    cout << "Enter fowl launch angle (deg): ";
    cin >> slope;
    slope = ((slope * pi) / 180.0); // convert to radians
    cout << "Enter fowl launch velocity (m/s): ";
    cin >> Velocity;
}

int main() {
    double t = 1.0; // time (s)
    double fowlY = 0.0; // object's height above ground (m)
    double fowlAngle = 0.0; // angle of launch of fowl (rad)
    double fowlVel = 0.0; // velocity of fowl (m/s)
    double fowlX = 0.0; // object's horiz. dist. from start (m)
    double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)
    double swineX = 0.0; // distance to swine (m)
    double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
    bool didHitSwine = false; // did hit the swine?

    srand(time(0));
    swineX = 50; //(rand() % 201) + 50;

    PrintIntro();

    GetUsrInpt(swineX, fowlAngle, fowlVel);


    // FIXME Make into a function called LaunchFaowl
    do {
        PrintUpdate(t, fowlX, fowlY);
        Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
        t = t + 1.0;
    } while (fowlY > 0.0); // while above ground
    PrintUpdate(t, fowlX, fowlY);


    fowlLandingX = fowlX;


    // FIXME Make into a function called DtrmnIfHit
    beforeSwineX = swineX - 30;
    if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
        cout << "Hit'em!!!" << endl;
        didHitSwine = true;
    }
    else {
        cout << "Missed'em..." << endl;
        didHitSwine = false;
    }

    return 0;
}

现在,我问的是这些节目在哪里误入歧途?我找不到任何理由,我搜索了所有变量,并且认为它们不应该是一个问题。我该如何解决?

注意:我不希望别人为我处理所有FIXME部分!我只是想知道我哪里出错了所以我可以照顾其余的人。

2 个答案:

答案 0 :(得分:0)

斜率必须通过引用传递:

void GetUsrInpt(double piggy, double& slope, double Velocity) { // FIXME    Make into a function called GetUsrInpt
   cout << "\nThe Mean Swine is " << piggy << " meters away.\n";
   cout << "Enter fowl launch angle (deg): ";
   cin >> slope;
   slope = ((slope * pi) / 180.0); // convert to radians
   cout << "Enter fowl launch velocity (m/s): ";
   cin >> Velocity;
 }

如果希望在函数中传递的变量中实际更改参数的值,则必须将其作为指针传递(传统上在C中)或在函数原型中将其声明为参考传递的参数(C ++) )如上所述。

你的教授通过参考x和y来改变函数内的变化:&amp; x&amp; y

void Trajectory(double t, double a, double v,
    double& x, double& y) {
    x = v * t * cos(a);
    y = v * t * sin(a) - 0.5 * grav * t * t;
    return;
}

对于在函数中更改其值的其他变量,以相同的方式工作,您将完成它。

答案 1 :(得分:0)

  

您已设置函数GetUsrInpt以将双参数传递为&gt;“按值传递”,因此只将swineX,&gt; fowlAngle和fowlVel的值的副本传递给函数。在&gt;函数中更改它们不会更改main函数中的值。如果你想&gt;更改它们并让主函数知道它,你必须通过使用指向变量的指针来传递&gt;引用。   像这样

void GetUsrInpt(double* ptrPiggy, double* pSlope, double* pVelocity) 
{
    //...
    cin >> *pPiggy;
    cin >> *pSlope;
    cin >>  *pVelocity;
    //...

}

//像这样调用函数:

GetUsrInpt( &swineX, &fowlAngle, &fowlVel );