[英]Using inputted variables in multiple functions in C++
I just started learning C++ from scratch last week and on my assignment I have to create a quadratic equation solver through a few files: assign1.cpp which contains my main(), funcs.cpp which contains every function, and a makefile. 我上周开始从零开始学习C ++,在我的任务中,我必须通过几个文件创建一个二次方程求解器:assign1.cpp包含我的main(),funcs.cpp包含每个函数,还有一个makefile。 I'm having trouble pulling everything together.
我把所有东西拉到一起都很困难。
I have to get the coefficients from a user and return them to a struct with three fields, and use their inputted information in a few functions to solve the equation. 我必须从用户那里获取系数并将它们返回到具有三个字段的结构,并在几个函数中使用它们的输入信息来求解方程式。
My programming is all over the place, and I got most of the errors to go away with coeff input; 我的编程到处都是,我得到的大部分错误都随着coeff输入消失了; in every function and input.variable_name_here for a, b, and c.
在每个函数和input.variable_name_here中为a,b和c。
Also, he wants us to use parameters. 此外,他希望我们使用参数。
I'm hardly half way done with the program. 我几乎没有完成该计划。
Here's what I've got: 这是我得到的:
//Program to solve quadratic equation
#include <iostream>
#include <cmath>
using namespace std;
//Structure for coefficients to be used
struct coeff {
double a;
double b;
double c;
};
//Prototypes?
double readCoeffs(), equSolver(), discr(), outResults();
//Read coefficients from user
double readCoeffs() {
coeff input;
cout << "Enter coefficient a: ";
cin >> input.a;
cout << "Enter coefficient b: ";
cin >> input.b;
cout << "Enter coefficient c: ";
cin >> input.c;
}
//Solve quadratic equation from user's input
double equSolver() {
coeff input;
/*
need to somehow get the discrim variable from discr() to this function
discr();
*/
double solution1 = (pow(input.b, 2) + sqrt(discrim)) / (2 * input.a);
double solution2 = (pow(input.b, 2) - sqrt(discrim)) / (2 * input.a);
}
//Solves discriminent of quadratic equation
double discr() {
coeff input;
double discrim = pow(input.b, 2) - (4 * input.a * input.c);
}
/*
//Display on the screen results of the calculation
outResults() {
if (//root exists)
cout << "Quadratic equation with the following coefficients: \n";
cout << "a: " << value << "; b: " << value << "; c: " << value << "\n" << endl;
cout << "has the following roots ";
cout << "Root1: " << value << "; Root2: " << value << "\n" << endl;
else
cout << "Quadratic equation with the following coefficients: ";
cout << "a: " << value << "; b: " << value << "; c: " << value << "\n" << endl;
}
*/
Your readCoeffs()
function declares the object input
. readCoeffs()
函数声明对象input
。 This means that input only exists within the function, so you can't use it elsewhere - it falls out of scope when the function finishes. 这意味着输入仅存在于函数中,因此您无法在其他地方使用它 - 当函数完成时,它不在范围之内。 Also, your function is declared with a
double
return-type, but doesn't return anything. 此外,您的函数使用
double
返回类型声明,但不返回任何内容。
Consider taking an argument as a reference to a coeff
struct, and declaring it as void
? 考虑将参数作为
coeff
结构的引用,并将其声明为void
?
void readCoeffs(coeff &input)
{
cout << "enter...";
cin >> input.a;
cin >> input.b;
cin >> input.c;
}
Then do the same for other functions (passing the struct), and declare your struct within your main()
function. 然后对其他函数(传递结构)执行相同的操作,并在
main()
函数中声明您的结构。
Edit: Added an example of a function with a return. 编辑:添加了带返回的函数示例。
double discr(coeff &input)
{
double discrim = pow(input.b, 2) - (4 * input.a * input.c);
return discrim;
}
I couldn't resist so I rewrote and reorganized the code to solve the issue. 我无法抗拒,所以我重新编写并重新组织代码以解决问题。 This compiles and does what you ask.
这会编译并执行您的要求。 I also fixed the quadratic formula because it should be:
我还修正了二次公式,因为它应该是:
(-b +/- (b^2 - 4ac)) / 2a (-b +/-(b ^ 2 - 4ac))/ 2a
and you had 你有
(b^2 +/- (b^2 - 4ac)) / 2a (b ^ 2 +/-(b ^ 2 - 4ac))/ 2a
#include <iostream>
#include <cmath>
using namespace std;
//Structure for coefficients to be used
struct coeff {
double a;
double b;
double c;
double sol1;
double sol2;
};
//Declarations
void readCoeffs(coeff &x);
void equSolver(coeff &x);
double discr(coeff x);
void outResults(coeff x);
//Read coefficients from user
void readCoeffs(coeff &input) {
cout << "Enter coefficient a: ";
cin >> input.a;
cout << "Enter coefficient b: ";
cin >> input.b;
cout << "Enter coefficient c: ";
cin >> input.c;
}
//Solve quadratic equation from user's input
void equSolver(coeff &input) {
double solution1 = ((-1 * input.b) + sqrt(discr(input))) / (2 * input.a);
double solution2 = ((-1 * input.b) - sqrt(discr(input))) / (2 * input.a);
input.sol1 = solution1;
input.sol2 = solution2;
}
//Solves discriminent of quadratic equation
double discr(coeff input) {
double d;
d = pow(input.b, 2) - (4 * input.a * input.c);
return d;
}
void outResults(coeff input) {
if( std::isnan(input.sol1) || std::isnan(input.sol2)) {
cout << "Quadratic equation with the following coefficients could not be solved: \n";
cout << "a: " << input.a << "; b: " << input.b << "; c: " << input.c << "\n" << endl;
} else {
cout << "Quadratic equation with the following coefficients: \n";
cout << "a: " << input.a << "; b: " << input.b << "; c: " << input.c << "\n" << endl;
cout << "has the following roots ";
cout << "Root1: " << input.sol1 << "; Root2: " << input.sol2 << "\n" << endl;
}
}
int main() {
coeff Coeff;
readCoeffs(Coeff);
equSolver(Coeff);
outResults(Coeff);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.