gpt4 book ai didi

C++ 将内容移动到函数中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:53 24 4
gpt4 key购买 nike

我正在做一个学校项目,我应该在其中获取各种“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 部分!我只想知道我哪里出错了,这样我就可以处理其余的问题。

最佳答案

You have set up the function GetUsrInpt to pass the double parameters as >"pass by value" there fore only copies of the values of swineX, >fowlAngle and fowlVel are passed to the function. Changing them in the >function won't change their values in the main function. If you want to >change them and have it known to the main function you have to pass by >reference by using the pointer to the variable. like this

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

}

//像这样调用函数:

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

关于C++ 将内容移动到函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33715453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com