How to solve a problem? Tell me please.

#include <conio.h> #include <iostream> #include <cmath> using std :: cout; using std :: cin; using std :: endl; int main() { setlocale(0, "russian"); double a, b, c; double d; double x, x2; cout << "Enter a,b,c" << endl; cout << "a="; cin >> a; cout << "b="; cin >> b; cout << "c="; cin >> c; d = (b*b) - (4 * a* c); cout << "D=" << d << endl; if (d < 0) cout << "No roots!" << endl; if (d == 0) x = -b / (2 * a); cout << "X=" << x << endl; if (d > 0) x = (-b + sqrt(d)) / (2 * a); cout << "X1=" << x << endl; x2 = (-b - sqrt(d)) / (2 * a); cout << "X2=" << x2 << endl; _getch(); return 0; } 

    1 answer 1

    With your condition if (d < 0) only output to the console that the equation has no roots. The variable x does not acquire any value, but you always try to output its value to the console. Your code clearly lacks curly brackets, try this solution:

     if (d < 0) { cout << "No roots!" << endl; } else if (d == 0) { x = -b / (2 * a); cout << "X=" << x << endl; } else { x = (-b + sqrt(d)) / (2 * a); cout << "X1=" << x << endl; x2 = (-b - sqrt(d)) / (2 * a); cout << "X2=" << x2 << endl; }