Quadratic equation issues!

Hello all, first time post.

Can anyone point out what I need to change in this??

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double a, b, c;
double s1, s2;
double disc;


cout << "Input values of a, b and c.\n\n";
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
cout << "c=";
cin >> c;

disc = ((b * b) - 4 * a * c);
if (disc > 0) {
s1 = (-1 * b + sqrt(disc)) / (2 * a);
s2 = (-1 * b - sqrt(disc)) / (2 * a);
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
}
else if(disc == 0)
{
s1 = s2 = (-1 * b) / (2 * a);
}
else
{
disc = (-1 * disc);
cout << "s1=" << "(" << (-1 * b) <<" + i" << sqrt(disc) <<") / "<< 2 * a;
cout << "s2=" << "(" << (-1 * b) <<" + i" << sqrt(disc) <<") / "<< 2 * a;
}
cout << "s1=" << s1 << "\n\n";
cout << "s2=" << s2 << "\n\n";
system ("pause");
return 0;
}
What exactly is the problem? Error, crash, screen freeze, incorrect output, or what? If it's an error, post the compiler's error report, please.
Last edited on
Ok it all looks good, but I reckon it fails at link time, even though sqrt() is in math.h you may need to specify it in your compiler command line, it will be a switch of some sort. If you reveal your IDE compiler then I may be able to find out.
closed account (10oTURfi)
@a142763
No it doesnt. He is outputing s1 which doesnt have any value, therfore causing crash.
Last edited on
@Krofna

It runs, but after I input the values the program skips some of the if else statements and only runs the last part.
What if disc was 0.5? Wouldn't that force the computer to find the square root of -0.5, returning "nan"?
Last edited on
He calculates both s1 and s2 in the first 2 disc cases, it's in the 3rd case where the values of those aren't calculated. So he is almost there!
He doesn't use s1 or s2 in the else statement. He has s1 and s2 in quotations. I really don't see why you need disc and those if statements. Just do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    // Initializations and input here.

    s1 = -b + sqrt((b * b - 4 * a * c) / (2 * a));
    s2 = -b - sqrt((b * b - 4 * a * c) / (2 * a));
    
    if(s1 != s1 || s2 != s2) // If the result is an imaginary number.
    {
        cout << endl << "The result is an imaginary number." << endl;
    }
    else // If the result is NOT an imaginary number.
    {
        cout << endl << "s1 = " << s1 << endl;
        cout << "s2 = " << s2 << endl;
    }
    
    system("pause");
    return 0;
}


No need for disc.
Last edited on
I'm so close!! Thanks for the help.

I'm struggling to get answers for imaginary numbers. I know that I need to include in the cout "+ or - i" but I don't know where to do it
For complex roots, you seem to be outputting the equation for computing the imaginary parts, not the computed numbers.

Also, you seem to be outputting results twice.

Say disc is negative.
Then the third if condition executes and you output the complex results.
But then after the program has passed through these three checks, you output s1 and s2 right before program termination.

Same thing if disc > 0.
You output s1 and s2 in the if condition, and then again right before program termination.
Topic archived. No new replies allowed.