gpt4 book ai didi

c++ - 用户定义的函数仅返回可能值中的 1 个

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

  1. I am attempting to write a program that calls functions to perform each task specified in my assignment. Everything seems to be working properly, except when i choose which form to use (the first step) it chooses choice 2, whether i choose 1 or 2. Please Help!
// Translates Linear equations from Point-Slope Form to Two-Point Form and Vice Versa
// Also takes given information and displays Slope-Intercept Form

#include <iostream>
#include <cmath>

using namespace std;

int getProblem();
double get2Pt(double& x1, double& y1, double& x2, double& y2);
double getPtSlope(double& x1, double& y1);
double slopeIntcptFrom2Pt(double& x1, double& y1, double& x2, double& y2);
double intcptFromPtSlope(double slope, double& x1, double& y1);
void display2Pt(double x1, double y1, double x2, double y2);
void displayPtSlope(double slope, double x1, double y1);
void displaySlopeIntrcpt(double slope, double yIntrcpt);

int main()
{
double x1, x2, y1, y2, slope, yIntcpt;
int choice;

choice = getProblem();

if (choice = 1)
{ slope = get2Pt(x1, y1, x2, y2);
yIntcpt = slopeIntcptFrom2Pt(x1, y1, x2, y2);
display2Pt(x1, y1, x2, y2);
displaySlopeIntrcpt(slope, yIntcpt);
}
else if(choice = 2)
{
slope = getPtSlope(x1, y1);
yIntcpt = intcptFromPtSlope(slope, x1, y1);
displayPtSlope(slope, x1, y1);
displaySlopeIntrcpt(slope, yIntcpt);
}
return 0;
}

int getProblem()
{
int choice;
cout << "Select the form you would like to convert to Slope-Intercept Form:" << endl
<< "1) Two-Point From (2 Points on the line are known)" << endl << "2) Point-Slope Form (The slope and one point on the line are known)" << endl;
cin >> choice;
return choice;
}
double getPtSlope(double& x1, double& y1)
{
double slope;
cout << "Enter the Slope =>" << endl;
cin >> slope;
cout << "Enter the X/Y Coordinates of the point, separated by a space =>" << endl;
cin >> x1 >> y1;
return slope;
}
double get2Pt(double& x1, double& y1, double& x2, double& y2)
{
double slope;

cout << "Enter the X/Y Coordinates of the First point, separated by a space =>" << endl;
cin >> x1 >> y1;
cout << "Enter the X/Y Coordinates of the Second point, separated by a space =>" << endl;
cin >> x2 >> y2;

slope = ((y2 - y1) / (x2 - x1));
return slope;
}
double slopeIntcptFrom2Pt(double& x1, double& y1, double& x2, double& y2)
{
double slope, yIntcpt;
slope = ((y2 - y1) / (x2 - x1));
yIntcpt = (slope * x1) + y1;
return yIntcpt;
}
double intcptFromPtSlope(double slope, double& x1, double& y1)
{
double yIntcpt;
yIntcpt = (slope * x1) + y1;
return yIntcpt;
}
void displayPtSlope(double slope, double x1, double y1)
{
if (y1 >= 0)
{
if (x1 >= 0) cout << "y - " << y1 << " = " << slope << "(x - " << x1 << ")" << endl << endl;
else cout << "y - " << y1 << " = " << slope << "(x + " << fabs(x1) << ")" << endl << endl;
}
else
{
if (x1 >= 0) cout << "y + " << fabs(y1) << " = " << slope << "(x - " << x1 << ")" << endl << endl;
else cout << "y + " << fabs(y1) << " = " << slope << "(x + " << fabs(x1) << ")" << endl << endl;
}
return;
}
void display2Pt(double x1, double y1, double x2, double y2)
{
cout << " " << y2 << " - " << y1 << endl << "m = ----------------------" << endl
<< " " << x2 << " - " << x2 << endl << endl;
}
void displaySlopeIntrcpt(double slope, double yIntrcpt)
{
if (yIntrcpt >= 0) cout << "y = " << slope << "x + " << yIntrcpt;
else cout << "y = " << slope << "x " << yIntrcpt;
return;
}

最佳答案

您正在将 1 分配给 choice 而不是比较是否相等:

if (choice = 1)

改成这个

if (choice == 1)
...
else if(choice == 2)

关于c++ - 用户定义的函数仅返回可能值中的 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712837/

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