//Write a program that prompts the user to input the value of a
//(the coefficient of x squared), b (the coefficient of x),
//and c (the constant term) and outputs the type of roots of the equation.
//Furthermore, if b squared - 4ac greater than/equal 0, the program
//should output the roots of the quadratic equation.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
float a, b, c;
float discriminant = (pow (b, 2) - 4 * a * c);
float positive = (((-b) + sqrt(discriminant)) / (2 * a));
float negative = (((-b) - sqrt(discriminant)) / (2 * a));
cout << "Enter a:"; //The coefficient of x squared
cin >> a;
cout << "Enter b:"; //The coefficient of x
cin >> b;
cout << "Enter c:"; //The constant term
cin >> c;
cout << "The discriminant is " << endl;
cout << discriminant << endl;
if (discriminant == 0)
{
cout << "The equation are single root." << endl;
}
elseif (discriminant < 0)
{
cout << "The equation are two complex root." << endl;
}
else
{
cout << "The equation are two real root." << endl;
}
return 0;
}
this appears to be an error for uninitialized variables. which makes sense, you don't give a,b,c values until after you tried to do computations with them.
This confuses some beginners. Note that variables in C++ are not really like the concept of a "variable" in math. They're similar, but in C++ you can't calculate "2 * x" symbolically, and then plug in the x afterwards. When "2 * x" is calculated, the value of x must already be known.
Basically, move your lines 16, 17, 18 to after the lines where you ask for input.