0

I've tried to check resources online but don't seem to quite manage to pass the result from a function into a 2nd one. I coded a program to solve the quadratic formula and need to use the discriminant (returned in the first function calculateDiscriminant) as an argument to the second function (quadraticFormula). How can I solve this? Thanks in advance!

#include <iostream>
#include <cmath> 
using namespace std;


double calculateDiscriminant(double a, double b, double c) {
    double discriminant;

    discriminant = pow(b,2) - (4 * a * c);
    
    return discriminant;
     }

void quadraticFormula(double discriminant, double a, double b, double c) {
    double result1;
    double result2;
    double result3;
    double discriminant = calculateDiscriminant(double a, double b, double c); //I feel my problem lies here
   

    if (discriminant > 0) {
        result1 = (-b + sqrt(discriminant))/(2 * a);
        result2 = (-b - sqrt(discriminant))/(2 * a);
        cout << "There are 2 solutions." << endl;
        cout << "The solutions are: " << result1 << " and " << result2;
    }
    else if (discriminant == 0) {
        result3 = (-b)/(2 * a);
        cout << "There is 1 solution." << endl;
        cout << "The solution is: " << result3;
        
    }
    else if (discriminant < 0) {
        cout << "There is no solution.";
    } }

int main() {
    
    double userInput1;
    double userInput2;
    double userInput3;
    double discriminant;
    
    cout << "Please enter the values of a, b, and c: " << endl;
    cin >> userInput1 >> userInput2 >> userInput3;
    
    calculateDiscriminant(userInput1, userInput2, userInput3);
    quadraticFormula(discriminant, userInput1, userInput2, userInput3);
    
    return 0; }

2 Answers2

1

You are mixing up the syntax to declare a function, with the syntax to call a function. But these are quite different things, try this

double discriminant = calculateDiscriminant(a, b, c);

Think about it this way, when you decalre a function you have to tell the compiler all the types, so

double calculateDiscriminant(double a, double b, double c) {

tells the compiler that calculateDiscriminant is a function with the double parameters that also returns a double. Now you've told the compiler that you don't need to repeat the information every time you call the function, so just

calculateDiscriminant(a, b, c)

is all you need to call the function. Strange this is that you got this right in every other function call in the program, so I'm not sure why you tried to do it differently here.

You have some other errors. In main this

double discriminant;

cout << "Please enter the values of a, b, and c: " << endl;
cin >> userInput1 >> userInput2 >> userInput3;

calculateDiscriminant(userInput1, userInput2, userInput3);
quadraticFormula(discriminant, userInput1, userInput2, userInput3);

should be this

cout << "Please enter the values of a, b, and c: " << endl;
cin >> userInput1 >> userInput2 >> userInput3;

quadraticFormula(userInput1, userInput2, userInput3);

Not sure why you are trying to call calculateDiscriminant twice. You don't need to.

And this

void quadraticFormula(double discriminant, double a, double b, double c) {

should be this

void quadraticFormula(double a, double b, double c) {

You are calculating the discriminant in the quadraticFormula function, so you don't need to pass it as parameter as well.

You should think about function parameters logically. A function that is calculating a quadratic formula needs three parameters, the coefficients of the three terms in the quadratic equation. If you find yourself writing a quadratic formula function that takes four parameters you are doing something wrong. That's simple logic, nothing to do with C++.

john
  • 85,011
  • 4
  • 57
  • 81
-2
void quadraticFormula(double a, double b, double c) ' 

//You Dont need 4th parameter

 {
        double result1;
        double result2;
        double result3;
        double var1=a; //here
        double var2=b; //here
        double var3=c; //here

//Assign Values from Parameters to Variables

    `double discriminant1=calculateDiscriminant(var1,var2,var3);` 

//just pass values to the fucntion calculateDiscriminant()

    if (discriminant1 > 0) {
        result1 = (-var2 + sqrt(discriminant1))/(2 * var1);
        result2 = (-var2 - sqrt(discriminant1))/(2 * var1);
        cout << "There are 2 solutions." << endl;
        cout << "The solutions are: " << result1 << " and " << result2;
    }
    else if (discriminant1 == 0) {
        result3 = (-var2)/(2 * var1);
        cout << "There is 1 solution." << endl;
        cout << "The solution is: " << result3;
        
    }
    else if (discriminant1 < 0) {
        cout << "There is no solution.";
    } }