Problem with my Solving Quadratic Equations Program

I don't quite get what is wrong with my program, well, I believe the error is a matter of {}, however, I'm having trouble determining the correct spot to place them.

error: expected primary=expression before 'else'
error: expected `;' before 'else'

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
#include <iostream>
#include <cmath>

using namespace std;

int main() {

	double a, b, c, x;

	cout << "Enter coefficient A (e.g. Ax^2 + Bx + C): ";
	cin >> a; // read input into a, b and c input from user
	
	cout << "Enter coefficient B (e.g. Ax^2 + Bx + C): ";
	cin >> b; // read input into a, b and c input from user
	
	cout << "Enter coefficient C (e.g. Ax^2 + Bx + C): ";
	cin >> c; // read input into a, b and c input from user

	double disc = (b*b) - (4 * a*c);  // discriminant

if (disc < 0) 
		
		cout << "No real solutions";  // discriminant is negative so no real solutions	  
				
else if (disc = 0)

		x = (-b - sqrt((b*b) - (4*a*c))) / (2*a);
	
		cout << "Solution is x = " << x;  // unique solution

else (disc > 0) {	
 	 
    double x1, x2;
			
        x1 = (-b + sqrt((b*b) - (4*a*c))) / (2*a);
			
	x2 = (-b - sqrt((b*b) - (4*a*c))) / (2*a);

    cout << "Solutions are x1 = " << x1 << ", x2 = " << x2;
                }
    cout << "." << endl;
			
return 0; }
Last edited on
else doesn't have a condition, it's everything else. Why don't you use else if like you do just before it? You're also missing brackets for your else if statement, if you're gonna have 2+ lines of code, you need brackets. I prefer having brackets at all times so if you want to go back and add things you'll avoid messing up.

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
#include <iostream>
#include <cmath>

using namespace std;

int main() {

	double a, b, c, x;

	cout << "Enter coefficient A (e.g. Ax^2 + Bx + C): ";
	cin >> a; // read input into a, b and c input from user
	
	cout << "Enter coefficient B (e.g. Ax^2 + Bx + C): ";
	cin >> b; // read input into a, b and c input from user
	
	cout << "Enter coefficient C (e.g. Ax^2 + Bx + C): ";
	cin >> c; // read input into a, b and c input from user

	double disc = (b*b) - (4 * a*c);  // discriminant

if (disc < 0) 
		
		cout << "No real solutions";  // discriminant is negative so no real solutions	  
				
else if (disc = 0) // changed to == from =, = is an assignment operator, == is for comparison
{ // added brackets
		x = (-b - sqrt((b*b) - (4*a*c))) / (2*a);
	
		cout << "Solution is x = " << x;  // unique solution
}// added brackets
else if (disc > 0) {	// change to else if http://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm
 	 
    double x1, x2;
			
        x1 = (-b + sqrt((b*b) - (4*a*c))) / (2*a);
			
	x2 = (-b - sqrt((b*b) - (4*a*c))) / (2*a);

    cout << "Solutions are x1 = " << x1 << ", x2 = " << x2;
                }
    cout << "." << endl;
			
return 0; }

Last edited on
Lines 26 and 29 need to be surrounded by curly braces.

Line 25 needs to compare equality instead of assigning 0 to disc.
@TarikNeaj and @cire, thanks for helping me out. Also, thanks for providing the tutorialspoint.com link, really helpful!
Oh, wait. It appears I have another error... The program runs well, however, when I enter my coefficient where the discriminant = 0 such as 1, 2, 1, my output is ".".

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
45
#include <iostream>
#include <cmath>

using namespace std;

int main() {

	double a, b, c;

	cout << "Enter coefficient A (e.g. Ax^2 + Bx + C): ";
	cin >> a; // read input into a, b and c input from user
	
	cout << "Enter coefficient B (e.g. Ax^2 + Bx + C): ";
	cin >> b; // read input into a, b and c input from user
	
	cout << "Enter coefficient C (e.g. Ax^2 + Bx + C): ";
	cin >> c; // read input into a, b and c input from user

	double disc = (b*b) - (4 * a*c);  // discriminant

if (disc < 0) 
	
		cout << "No real solutions";  // discriminant is negative so no real solutions	  
				
else if (disc = 0)
{
	double x;

	x = (-b - sqrt((b*b) - (4*a*c))) / (2*a);
	
	cout << "Solution is x = " << x;  // unique solution
}
else if (disc > 0) 
{		
    double x1, x2;
			
    x1 = (-b + sqrt((b*b) - (4*a*c))) / (2*a);
			
	x2 = (-b - sqrt((b*b) - (4*a*c))) / (2*a);

    cout << "Solutions are x1 = " << x1 << ", x2 = " << x2;
}
    cout << "." << endl;
			
return 0; }
You're still using assignment on line 25 instead of comparison.
That's because you missed a part in my solution, and @cite's comment too.

@cire wrote:
Line 25 needs to compare equality instead of assigning 0 to disc.


I wrote:

else if (disc = 0) // changed to == from =, = is an assignment operator, == is for comparison

Change it to - else if(disc == 0) // note ==
Last edited on
OH! My bad. Thanks you guys. =)
Topic archived. No new replies allowed.