0

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;

}

*/
1
  • It might help if you could actually ask a question. Try to be as precise as possible, e.g., “the compiler complains that discr does 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. Commented Jan 31, 2014 at 2:31

2 Answers 2

2

Your readCoeffs() function declares the object 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.

Consider taking an argument as a reference to a coeff struct, and declaring it as 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.

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;
}
Sign up to request clarification or add additional context in comments.

17 Comments

When I try that it's saying the readCoeffs it's an incomplete type and is not allowed, the the coeff type name is not allowed, and that it expects a ) and a ;
Did you also change the prototype to void, and add the argument?
If void readCoeffs(); is the correct way to instantiate a prototype then yes. It says that the declaration is incompatible. I tried it without the prototype as well
You'd need readCoeffs(coeffs &input); as your prototype if you want to pass the argument too (Which you'll need to if you want to change the struct variables)
Although if you don't have the prototype for readCoeffs, provided your struct is defined before your function, and your function before you use it, that shouldn't matter...
|
0

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

and you had

(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);

}

3 Comments

I'll definitely take a look at this. Thank you for taking the time to help!
Regarding something being 'off', perhaps problems occur if descr returns a negative number, since equSolver has no knowledge of complex numbers? (Although perhaps that's what your isnan(...) section handles?)
The formula was off, I fixed it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.