NEED HELP FAST!!

In this program im writing I need to be able to print to the screen "NO REAL ROOTS" if my quadratic has a negative descriminant. This is what I have so far will someone please help me? Im bout to beat my computer up!!!

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main ()

{
//variables

double a, b, c;
double x1, x2;

//ask user to input values for variables.

cout << "Input a value for the variable a.";
cin >> a;

cout << "Input a value for the variable b.";
cin >> b;

cout << "Input a value for the variable c.";
cin >> c;

//math stuff

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

x2 = ((-b)-sqrt((b*b)-(4*a*c))/(2*a));

//if statement to put out NO REAL ROOTS

if( x1 < 0 )
cout << "NO REAL ROOTS" << endl ;

else
cout << x1 << endl;

if( x2 < 0 )
cout << "NO REAL ROOTS" << endl ;

else
cout << x2 << endl;

return 0;

}
Last edited on
Hint: A quadratic will have a negative discriminant if the b^2 - 4ac portion is less than zero.
Well, actually, I don't know the response of an arrel on C++ if it's negative. Beside, you could calculate before:
1
2
3
4
5
6
double aux = (b*b)-(4*a*c))/(2*a);

if aux < 0
          cout << "NO REAL ROOTS" << endl ;
else
          //calcul the answer or whatever 
Okay thank yall. @ iHutch105: i tried setting my if statement as this if(b^2 - 4ac < 0) cout << "NO REAL ROOTS"; but it still didnt print it out to the screen
My current Mac doesn't have XCode or g++ installed so I can't test this right now, otherwise I'd check it out. I'm heading out to a family birthday get-together so I'll be out for a few hours too.

I'll set an XCode download away and take a look later on.

In the meantime, the only advice I can offer is to print yourself some debug statements. For example, before comparing something in an if statement, cout the value of what your comparing:

1
2
3
4
5
6
cout << "Debug x: " << x << endl;

if (x < 0)
{
  // Do stuff
} 


That way, you can see what x is right before you hit your statement and you may be able to figure out why the code you want isn't executing.
Last edited on
Okay thank you!
@iHutch105 did you ever figure out the problem? I'm currently working on it right now and im still not getting the results i need..
closed account (D80DSL3A)
The discriminant is the b*b - 4*a*c part, so you want:
1
2
3
4
5
6
7
8
9
10
double disc = b*b - 4*a*c;
if( disc < 0.0 )
{
    cout << "NO REAL ROOTS" << endl;
}
else
{
    x1 = ( -b + sqrt(disc) )/(2*a);
    // etc.
}
Hey, sorry, I had a crazy weekend that involved a 6 hour stint in the emergency department of a hospital. Fun times.

That said, I'm back at work today and have access to Visual Studio here. I'll take a look on my dinner break in a couple of hours or so.
Right, got it.

There's two problems here.

Lets address this first: the quadratic formula in your code is off. Remember, the whole equation is over 2a and, from the looks of the brackets there, I'd say that isn't happening. Precedence can be ugly in C++, so sometimes it's good to split things up. Here's how I did the quadratic equation.

1
2
x1 = (-b + (sqrt((b*b) - (4*a*c))));
x1 = x1 / (2*a);


For me, it makes things a little clearer and makes sure I'm getting what I want.

Secondly, is your roots issue. Basically, if there's no real roots, then there's no point doing the equation. So, you may want to check for real roots before even thinking about getting into the meatier stuff.

1
2
3
4
// Declare variables
// Get inputs from user
// Check roots before solving
// Solve equation if needed 


So, in the above, I created another double called discriminant and set that to the sum of b^2 - 4ac. We know that if the discriminant is less than zero, there's no real roots, so once I had the discriminant, I just added a check.

1
2
3
4
5
6
7
8
9
10
11
if (discriminant < 0)
{
   cout << "Equation has no real roots" << endl;
   return 0;
}
else
{
   // Perform calculations here
   // Output answers
   return 0;
}


Hope this helps.
Topic archived. No new replies allowed.