if statements not working

I'm trying out this program to solve quadratics for you, and I got it all to work. But i realized I needed to add some If statements for when a certain part of it is positive or negative. But when I added them in, instead of going to one of the, it skipped them both completely and continued. Here's what I have:
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

int a;
int b;
int c;
char redo;

int main()
{
do
{
system("cls");

cout << "Please enter your 'a' term (ax^2 + bx + c =0)" << endl;
cin >> a;

cout << "Please enter your 'b' term." << endl;
cin >> b;

cout << "Please enter your 'c' term." << endl;
cin >> c;

if(sqrt((b * b) -4 * a * c) < 0)
{
cout << "No x intercepts" << endl;
}

else if(sqrt((b * b) - 4 * a * c) > 0)
{
cout << "x=" << (-b + sqrt((b * b) - 4 * a * c)) / (2 * a) << endl;
cout << "x=" << (-b - sqrt((b * b) - 4 * a * c)) / (2 * a) << endl << endl;
}

cout << "Would you like to try again?(Y/N)" << endl;
cin >> redo;
}

while(redo=='y'||redo=='Y');

return 0;
}

I've never done anything with ifs like this before I've only done simple ones not a big expression so I just need some help. Thanks
You are comparing a double value (sqrt produces a double) with an integer, which will never work.

Google C++ floating point comparisons to see how to do it properly.

Also rather than using the do loop, use a while loop with the end condition controlled by a bool variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <locale>

using std::cin;
using std::cout;
using std::endl;

bool Quit = false;
char redo;

while(!Quit) {

//your code here

    cout << "Would you like to try again?(Y/N)" << endl;
    cin >> redo;

    redo = std::toupper(redo);

//user wants to quit
    if (redo == 'Y')
         Quit = true;
}  //end of while loop


Notice how I used the code tags - with the <> formatting button on the right.

HTH
Last edited on
I get what your saying and it makes sense, but I'm not quite finding out how to make it work. I need that expression in an if statement saying if its positive or negative (if its possible)
Did you google and read some articles?
Umm.. look at your parenthesis placement:

 
if(   sqrt(    (b * b) -4 * a * c    ) < 0)


The square root of an expression will never be less than zero.

You probably meant this:

 
if(  (    sqrt(b * b) -4 * a * c    ) < 0)
Last edited on
yeah i did couldnt find it for some reason
that is what i mean but it still isn't working i'm thinking it might work if i just put b^2 - 4ac in there because thats actually what needs to be positive or negative...
ok what i tried there worked completely fine. I had a big feeling it was something small like that that i had just overlooked....
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm


Why is it that people don't seem to know how to use google?

http://en.wikipedia.org/wiki/Quadratic_equation


As Disch was alluding to you approach is wrong because sqrt always produces a positive number .

Why not just work out the 2 roots with a function that takes the determinant -> sqrt((b * b) -4 * a * c) as a parameter. Call the function twice with +ve & -ve versions of the determinant.

However @Disch:

if( ( sqrt(b * b) -4 * a * c ) < 0)

is not how the quadratic equation works, and sqrt(b * b ) is just b.

HTH
yes i realized that sqrt(b * b) is just b but i was just kind of sticking with the actual equation (x = (-b +- sqrt(b^2 - 4ac)) / (2a)).... but yes i saw that article but didnt see how it really helped
Doh yeah I didn't actually see that. Blargg
Why not just work out the 2 roots with a function that takes the determinant -> sqrt((b * b) -4 * a * c) as a parameter. Call the function twice with +ve & -ve versions of the determinant.


That was the helpful suggestion I was making.
Topic archived. No new replies allowed.