I was wondering if anyone could help me with this (beginner) problem. I am creating a very basic quadratic equation calculator. I have listed my code below, alongside comments (the ones at the top of the code snippet explain what i have to do). I've looked online at multiple solutions as well as tried myself but it seems that i keep getting incorrect x1 and x2 values. If anyone could guide me I would be more than happy. Cheers.
/*
create program to calculate x for quadratic equation. (a, b and c)
1) create function which prints out roots of quad equation.
2) throw exception if b2 - 4ac is less than 0.
3) call the function from main.
*/
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double roots() { //return type, function name, parameters and body of function.
double a = 0, b = 0, c = 0, x1, x2;
double discriminant = (b * b) - 4 * a * c;
cout << "Enter roots of quad equation (in order: a, b, c) " << endl;
cin >> a >> b >> c;
if (discriminant >= 0) {
cout << "Your quadratic equation is: " << a << "x squared + " << b << "x + " << c << " = 0 " << endl;
x1 = -b + sqrt(discriminant) / (2 * a);
x2 = -b - sqrt(discriminant) / (2 * a);
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else {
cout << "Negative value returned from (b2 - 4ac), please try again! " << endl;
exit(1);
}
}
int main() {
cout << "Quadratic Equation Calculator " << endl;
roots();
return 0;
}