Ok, so i am creating a quadratic equation solver in c++ and cant seem to get the right output on imaginary numbers. the real number roots come out fine (e.g. x= 2 and x = 5) but when the imaginary numbers come out, something weird happens where it shows (x = -1.#IND)?? someone please help me figure this out? i want it to show something more like x = 5.287*i. Here is my code
#include <iostream>
#include <string>
#include <cmath>
#include <complex>
using namespace std;
int main() {
cout << "Project 4 (QUADRATIC EQUATION)\nLong Beach City College \nAuthor: Mathias Pettersson \nJuly 15, 2015\n" << endl;
cout << "This program will provide solutions for trinomial expressions.\nEXAMPLE: A*x^2 + B*x^2 + C = 0" << endl;
double a, b, c;
double discriminant;
//Variable Inputs
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
//Computations
discriminant = (b*b) - (4 * a * c);
double x1 = (((-b) + sqrt(discriminant)) / (2 * a));
double x2 = (((-b) - sqrt(discriminant)) / (2 * a));
//Output
if (discriminant == 0)
{
cout << "The discriminant is ";
cout << discriminant << endl;
cout << "The equation has a single root.\n";
}
else if (discriminant < 0)
{
cout << "The discriminant is ";
cout << discriminant << endl;
cout << "The equation has two complex roots.\n";
cout << "The roots of the quadratic equation are x = " << x1 << "*i, and" << x2 << "*i" << endl;
}
else
{
cout << "The discriminant is ";
cout << discriminant << endl;
cout << "The equation has two real roots.\n";
}
//Final Root Values
cout << "The roots of the quadratic equation are x = ";
cout << x1;
cout << ", ";
cout << x2 << endl << endl;
system("PAUSE");
return 0;
}