0

What is wrong with this?

Hello. I am a beginner in C++ language. I don't understand why the code for solving second power equations, runs, but outputs wrong solutions. Thanks in advance. #include <iostream> #include <math.h> using namespace std; int main() { int x1; int d; int a; int b; int c; int x2; int x; cin >>a >>b >>c; cout <<"a value " <<a <<endl <<"b value " <<b <<endl <<"c value " <<c <<endl ; int D = b * b - 4 * a * c ; // when it has a solution if (D > 0) { d= sqrt (D); x1= ( - b + d) / 2 * a ; x2 = ( - b - d) / 2* a ; cout <<"x1 = " <<x1 <<endl <<"x2 = " <<x2 ; } // when it has only one solution if (D=0){ x= -b / 2 * a ; cout <<" there is only one solution in wich x=" <<x ; } //when it doesn't have a solution if (D<0){ cout << "this equation has no solution"; } return 0; }

3rd Aug 2018, 4:45 PM
me2xgy
me2xgy - avatar
3 Respostas
+ 2
it should be if (D==0) instead of D=0... D=0 is not equality operator... it's assignment operator... for comparison, you need to use ==
3rd Aug 2018, 4:57 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Use paranthesis for precedence. I mean write 2*a as (2 * a) and than see the output. Is it coming true now ?
3rd Aug 2018, 5:02 PM
Meet Mehta
Meet Mehta - avatar
+ 1
Also if you have any query regarding code in future please try to post it on playground and paste its link to QnA to get more faster answers to your query.
3rd Aug 2018, 5:04 PM
Meet Mehta
Meet Mehta - avatar