gpt4 book ai didi

c++ - 我的功能不会交互。那或者它不会返回值

转载 作者:行者123 更新时间:2023-11-28 06:58:01 25 4
gpt4 key购买 nike

所以我一直在努力工作,但运气不佳。第一个函数正在工作,因为我通过对每个变量使用 cout 来确认它并且方程是正确的,因为我在没有函数的情况下尝试了它。问题是 function1engineer 没有为 distance 赋值,所以它变成了默认的 double 值。如何让 function1engineer 接受来自第一个函数的输入?

这是我的代码。

#include <iostream>
#include <cmath>
#include <math.h>
#include <iomanip>
#define PI 3.14159265
using namespace std;
//angle*PI/180 to work in c++ for angles.
void function1customer(double& x1, double& x2, double& y1, double& y2);
double function1engineer(double distance, double x1, double x2, double y1, double y2);

int main()
{
int exitLoop;
do
{


cout << "Choose an option.\n1: Given two points, compute the distance between them."
<< "\n2: Given two points, compute the horizontal angle from \nthe first point to the second."
<< "\n3: Given the elevation angle and velocity, compute the \n(horizontal) distance an object travels."
<< "\n4: Given a starting point, a distance, and a horizontal angle, \ncompute the destination point."
<<endl;
int opselect;
cin >> opselect;
switch(opselect)
{
//compute distance between 2 points
case 1:
double distance;
double x1, x2, y1,y2;
function1customer(x1,x2,y1,y2);
cout << x1 << endl << x2 << endl << y1<< endl << y2 << endl;
function1engineer;
cout << setprecision(2)<< distance << endl;
break;
}
cout << "Would you like to perform another calculation?\n0=No\n1=Yes" <<endl;
cin >> exitLoop;
}
while(exitLoop != 0);
}

void function1customer(double& x1, double& x2, double& y1, double& y2)
{
cout << "Enter X1 cordinate in feet" <<endl;
cin >> x1;

cout << "Enter X2 cordinate in feet" <<endl;
cin >> x2;

cout << "Enter Y1 cordinate in feet" <<endl;
cin >> y1;

cout << "Enter Y2 cordinate in feet" <<endl;
cin >> y2;
}

double function1engineer(double distance, double x1, double x2, double y1, double y2)
{
distance = 0;
distance = pow( ( pow((x2-x1),2.0) + pow((y2-y1),2.0) ),(1.0/2));
return distance;
}

最佳答案

您需要通过引用传递distance,并且不需要显式返回它。 (就像您对 function1customer 的参数所做的那样。使用这个:

void function1engineer(double& distance, double x1, double x2, double y1, double y2)

调用代码为:

function1engineer(distance,x1,x2,y1,y2);

关于c++ - 我的功能不会交互。那或者它不会返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22905840/

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