C++将东西移到一个不起作用的函数中
C++ Moving stuff into a function not working
我正在做一个学校项目,在这个项目中,我应该学习各种"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;
}
对其他变量使用相同的方法,在函数中更改它们的值,就可以完成它。
相关文章:
- 我正在尝试解决一个需要数组总和值但代码不起作用的问题,我想做这样的事情
- 反转一个数字程序不起作用,为什么?
- 我有一个应该打印数组的函数,但由于某种原因它不起作用
- 当我尝试在函数内声明一个函数时,它不起作用?有什么建议吗?*源代码如下*
- 从类中的另一个文件请求函数不起作用
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- 即使我正在.cpp文件中实例化一个伪对象,.cpp文件内模板函数的定义也不起作用
- 将字符从一个字符串简单分配到另一个字符串不起作用
- 试图找到一个数字的平方根,但代码不起作用。C++
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- 我正在编写一个拆分为 3 的合并排序,我不知道为什么它不起作用
- 我试图键入一个函数来在单链表的末尾添加一个节点,但不起作用
- VS为我提供了对构造函数的另一个解决方案,但我想知道为什么我的工作不起作用
- 一个'case'在简单的'switch'语句中不起作用
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 将布局从一个类添加到另一类不起作用
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++ 为什么这些构造函数调用中有 3 个有效,但有一个不起作用?
- 为什么其中一个模板静态函数有效,而另一个不起作用
- C++将东西移到一个不起作用的函数中