### i am having trouble with this

I keep getting an error with this code can any one help me out

if ((b*b-4*a*c) < 0) && (a!=0)
 ``1234`` ``````if ((b*b-4*a*c) < 0) && (a!=0){ blah blah blah } ``````

You need a ) at the end, that will end the if()

 ``123`` ``````if ((b*b-4*a*c) < 0) && (a!=0)){ blah blah blah }``````

Think thats right.
closed account (D80DSL3A)
Still one short!
`if ( ((b*b-4*a*c) < 0) && (a!=0) )`
I am having trouble getting the correct answers with tis code. can anyone check to see if i have it correct.

#include <iostream>// You can skip the places which are used for the
#include <cmath>// repeat for now.
#include <string>
#include <iomanip>

using namespace std;
bool Repeat = true;//using for repeat
int main ()

{
cout << fixed << showpoint << endl;
cout << setprecision (4);
cout << scientific;
char symbol;//using for repeat
float a,b,c,i,disc;
float x1,x2;
while (Repeat){//also for repeat
cout << "This programm finds the roots of a quadratic equation" << endl;
cout << "ax^2 +- bx +- c" << endl;

cout << "Enter values, a,b and c: " << endl;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << "c: ";
cin >> c;
disc = ((b*b) -4*a*c);

if (a==0)
{
cout << "The answer is undefined. " << endl;
cin >> symbol;//Skip
Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;//And again

}

if (disc > 0)
{
cout << "The discriminant is: " << disc << endl;
x1 = (-b +sqrt((b*b) -4*a*c))/2*a;
x2 = (-b -sqrt((b*b) -4*a*c))/2*a;
}
else if (disc ==0)
{
cout << "The discriminant is: " << disc << endl;
x1=x2 =(-1*b)/(2*a);

}
else
{
disc =-1*disc;
cout << "No answer. Discriminant is less than 0"<< endl;
cout << "Would you like to calculate anyway? (Y or N): ";//Skip
cin >> symbol;//Skip
Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;//And again

cout << "Imaginary solution X1: " << x1 << endl;
cout << "Imaginary solution X2: " << x2 << endl;
cout << "Would you like to calculate again (Y or N): ";//Skip
cin >> symbol;//Skip
Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;//And again

}

}
return 0;

}
I believe the problem are the missing parentheses around 2*a in these lines:

 ``12`` ``````x1 = (-b +sqrt((b*b) -4*a*c))/2*a; x2 = (-b -sqrt((b*b) -4*a*c))/2*a;``````

 `[code][/code]`
tags.
Last edited on
i get that is the problem but how would i be able to fix it
You should add the parentheses around 2*a...

 ``12`` ``````x1 = (-b +sqrt((b*b) -4*a*c))/(2*a); x2 = (-b -sqrt((b*b) -4*a*c))/(2*a);``````
the parenthesis didn't work. I am still coming up with the wrong answer
I ran the program and got the correct answer. I did have to add some lines to output x1 and x2.
what where the lines that you added
 ``12345678`` ``````if (disc > 0) { cout << "The discriminant is: " << disc << endl; x1 = (-b +sqrt((b*b) -4*a*c))/(2*a); x2 = (-b -sqrt((b*b) -4*a*c))/(2*a); cout << "x1 = " << x1 << endl; cout << "x2 = " << x2 << endl; }``````
Topic archived. No new replies allowed.