Good day. Only recently began to learn programming. I set myself a task, to create a program for calculating the solution of quadratic equations, a discriminant. The program seems to be working properly, but has encountered the following problem, when entering a = 0, b = 0, c = 0, it gives an error about closing the program. How to avoid it? I apologize in advance if not so designed the topic.
#include <iostream> #include <math.h> using namespace std; int main() { int a, b, c; double x1, x2, x12, D; cout << "Let's try to solve the equation of the form ax^2+bx+c=0" << endl; cout << " Pls enter a: "<< endl;; cin >> a; cout << "Pls enter b: "<< endl;; cin >> b; cout << "Pls enter c: "<< endl;; cin >> c; if ((a == 0) && (b == 0) && (c == 0)) { cout << " does not exist " << endl;} // a*x^2+b*x+c=0 D = (pow(b,2))-4*a*c; cout << "D= " << D; if (D > 0) { x1 = ((-b) + sqrt(D)) / (2 * a); cout << " x1= " << x1; x2 = ((-b) - sqrt(D)) / (2 * a); cout << " x2= " << x2; } else { if (D == 0) { x12 = -b / (2 * a); cout << " x12= " << x12; } else { if (D < 0) cout << " x1,2 does not exist "; } } return 0; }
if ((a == 0) && (b == 0) && (c == 0))
come from? - Grundyx12 = -b / (2 * a);
division by 0. I think a debugger would help here. By the way, it's enough to check that a! = 0. - pavel0
? - Grundy2*a
- Grundy