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'
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.
#include <iostream>
#include <cmath>
usingnamespace 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
elseif (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
elseif (disc > 0) { // change to else if http://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htmdouble 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; }
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 ".".