//
// Created by Fredrick Park on 2/17/17.
//
/*
 * Quadratic.cpp
 * This program finds roots of the quadratic equation
 * a x^2+ bx + c = 0
 * if a is zero or if the equation has no real roots, the
 * program exits while printing an error message*/


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

// function prototypes
void getCoefficients(double & a, double & b, double & c);
void solveQuadratic(double a, double b, double c,
                    double & x1, double & x2);
void printRoots(double x1, double x2);
void error(string msg);

// main program
int main() {
    double a, b, c, r1, r2;
    cout << "OG a, b, c = : " << a << " " << b << " " << " " << c << endl;
    getCoefficients(a, b, c);
    cout << "after user entered a, b, c = : " << a << " " << b << " " << c << endl;

    solveQuadratic(a, b, c, r1, r2);
    printRoots(r1, r2);
    return 0;
}

void getCoefficients(double & a, double & b, double & c) {
    cout << "Enter coefficients for the quadratic equation:" << endl;
    cout << "a: ";
    cin >> a;
    cout << "b: ";
    cin >> b;
    cout << "c: ";
    cin >> c;
}

void solveQuadratic(double a, double b, double c,
                    double & x1, double & x2) {
    if (a == 0) error ("The coefficient a must be nonzero.");
    double disc = b * b - 4 * a * c;
    if (disc < 0) error ("This equation has no real roots.");
    double sqrtDisc = sqrt(disc);
    x1 = (-b + sqrtDisc) / (2 * a);
    x2 = (-b - sqrtDisc) / (2 * a);
}

void printRoots(double x1, double x2){
    if (x1 == x2) {
        cout << "There is a double root at " << x1 << endl;
    } else {
        cout << "The roots are " << x1 << " and " << x2 << endl;
    }
}

void error(string msg){
    cerr << msg << endl;
    exit(EXIT_FAILURE);
}




