whats the wrong in this code (solution for 2nd degree function) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

whats the wrong in this code (solution for 2nd degree function)

#include <iostream> #include <cmath> using namespace std; int main(){ cout << "please enter a, b , c \n"; double a, b, c, d, x1, x2; cin >> a >> b >> c; if (a = 0) x1 = x2 = -c / b; else d = pow(b, 2) - 4 * a*c; if (d > 0) cout << "there is no real root \n"; else { x1 = (-b + sqrt(d)) / 2 * a; x2 = (-b - sqrt(d)) / 2 * a; cout << "the root is real \n "; cout << "x1=" << x1 << "x2=" << x2; } return 0;

24th Dec 2017, 9:37 PM
Ahmed Alsobky
Ahmed Alsobky - avatar
1 Answer
0
Hmm, let see... At first you have to use == in if() to check equality a to 0, not = . Then there is no any exception for case when user entering both a and b as 0, so in this case you have abnormal termination caused by division by zero in x1 = x2 = -c/b. Also you forget } after return 0. Hope this will help! :)
24th Dec 2017, 10:42 PM
alessio paulo
alessio paulo - avatar