Quadratic Equation

I get a C4700 with a, b, c uninitialized local variable for Line 16.
What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//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>
using namespace 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;
	}
	else if (discriminant < 0)
	{
		cout << "The equation are two complex root." << endl;
	}
	else
	{
		cout << "The equation are two real root." << endl;
	}

	return 0;
}
Last edited on
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.
Last edited on
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.
Topic archived. No new replies allowed.