C ++将内容移入功能不起作用

丹尼

我正在做一个学校项目,在该项目中,我应该学习各个“ 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部分!我只想知道哪里出了问题,以便我照顾其余的人。

琳达·P。

您已经将函数GetUsrInpt设置为将双精度参数传递为>“按值传递”,因此只有swineX,> fowlAngle和fowlVel值的副本才传递给该函数。在>功能中更改它们不会更改主功能中的值。如果要更改它们并使主要功能知道它,则必须使用指向变量的指针> reference传递。像这样

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

}

//这样调用函数:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

$ .ajax错误功能不起作用

来自分类Dev

Lua C API:使用lua_pushcfunction推送功能不起作用

来自分类Dev

JavaScript的替换功能不起作用

来自分类Dev

异步功能不起作用

来自分类Dev

Ng重复功能不起作用

来自分类Dev

scons命令功能不起作用

来自分类Dev

CSS:悬停功能不起作用

来自分类Dev

删除功能不起作用

来自分类Dev

JavaScript onClick功能不起作用

来自分类Dev

简单的计算功能不起作用

来自分类Dev

当我们在比较器函数中使用等号“ =”时,为什么c ++的内置排序功能不起作用?

来自分类Dev

重命名和删除txt文件的功能不起作用C ++

来自分类Dev

Ubuntu 19.10上带有vscode和C#扩展名的Unity3D:出现错误,自动完成功能不起作用

来自分类Dev

将节点添加到链表的功能不起作用 C

来自分类Dev

为什么此交换功能不起作用?(在C中交换字符串)

来自分类Dev

C ++朋友功能不起作用

来自分类Dev

功能不起作用

来自分类Dev

功能不起作用

来自分类Dev

简单选择功能似乎不起作用(C ++)

来自分类Dev

向量擦除功能不起作用C ++

来自分类Dev

功能不起作用(不打印任何内容)

来自分类Dev

加载动态内容后jQuery功能不起作用

来自分类Dev

C-编译时没有错误,但功能不起作用

来自分类Dev

角度将数据推入功能不起作用

来自分类Dev

C#模糊功能不起作用

来自分类Dev

C ++堆栈实现弹出功能不起作用

来自分类Dev

C# 拆分功能不起作用

来自分类Dev

mysql c api mysql 查询功能不起作用

来自分类Dev

我正在尝试通过 c # 创建队列类,为什么出队功能不起作用?