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

C++ Moving stuff into a function not working

本文关键字:一个 不起作用 函数 C++      更新时间:2023-10-16

我正在做一个学校项目,在这个项目中,我应该学习各种"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部分!我只想知道我哪里出了问题,这样我就可以照顾他们了。

您已经将函数GetUsrInput设置为将双参数传递为>"pass-by-value",因此只有swineX、>fowlAngle和fowlVel的值的副本会传递给该函数。在>函数中更改它们不会更改它们在主函数中的值。如果你想>更改它们并让主函数知道它,你必须通过使用指向变量的指针来传递>引用。像这个

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

//调用这样的函数:

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

斜率必须通过引用:

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,在函数中发生变化:&x&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;
}

对其他变量使用相同的方法,在函数中更改它们的值,就可以完成它。

相关文章: