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. 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; in every function and input.variable_name_here for a, b, and 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;
}
*/
discrdoes not return a value, why does it not just pick the last value computed?” – you may be able to help yourself much faster than anyone else could, with a little training and effort.