Quadratic equations problem

Nov 22, 2013 at 2:23pm
closed account (ihASLyTq)
Please help. I'm student and now working on the program that can solve quadratic equations. The text that appears on screen and solutions should depend from discriminant, but it doesn't.

Also, if discriminant less than zero, it doesn't show that there are no solutions. Instead it shows "-2147387..."

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
26
27
28
#include <iostream>
#include <math.h>

using namespace std;

int main ()
{
    int a, b, c, x, D, result1, result2, result3;
    cout << "Enter coefficient of x^2: " << endl;
    cin >> a;
    cout << "Enter coefficient of x: " << endl;
    cin >> b;
    cout << "Enter the third component: " << endl;
    cin >> c;
    D = ( b * b ) - ( 4 * a *c );
    if (D > 0)
        cout << "This equation has 2 solutions." << endl;
    result1 = ( 0 - b + sqrt (D) ) / (2 * a);
    cout << "The first solution is: " << result1 << endl;
    result2 = ( 0 - b - sqrt (D) ) / (2 * a);
    cout << "The second solution is: " << result2 << endl;
    if (D = 0)
        cout << "This equation has only solution." << endl;
    result3 = ( 0 - b ) / (2 * a);
    cout << "The solution is: " << result3 << endl;
    if (D < 0)
        cout << "This equation has no solutions." << endl;
}
Last edited on Nov 22, 2013 at 2:24pm
Nov 22, 2013 at 2:39pm
You're missing braces from your solutions. There's also the odd syntax error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if( D > 0 )
{
    // Discriminant greater than zero
    // Two real solutions code here
}
else if ( D == 0 )
{
   // Discriminant is zero
   // Code for the one real solution here
}
else
{
   // Discriminant is less than zero
   // No real roots
}
Nov 22, 2013 at 2:43pm
The variables should be of type double instead of int.

if (D = 0) here the = is the assignment operator, it should instead be == to test for equality.

The header should be #include <cmath> rather than <math.h>
Nov 22, 2013 at 2:48pm
closed account (ihASLyTq)
Thanks, now it works better, but in all three situations after the correct text it shows "This equation has no solutions".

Here's edited code:
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
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <math.h>

using namespace std;

int main ()
{
    int a, b, c, x, D, result1, result2, result3;
    cout << "Enter coefficient of x^2: " << endl;
    cin >> a;
    cout << "Enter coefficient of x: " << endl;
    cin >> b;
    cout << "Enter the third component: " << endl;
    cin >> c;
    D = ( b * b ) - ( 4 * a *c );
    if (D > 0)
    {
        cout << "This equation has 2 solutions." << endl;
        result1 = ( 0 - b + sqrt (D) ) / (2 * a);
        cout << "The first solution is: " << result1 << endl;
        result2 = ( 0 - b - sqrt (D) ) / (2 * a);
        cout << "The second solution is: " << result2 << endl;
    }
    else if (D == 0)
    {
        cout << "This equation has only solution." << endl;
        result3 = ( 0 - b ) / (2 * a);
        cout << "The solution is: " << result3 << endl;
    }
    else;
    {
        cout << "This equation has no solutions." << endl;
    }
}
Last edited on Nov 22, 2013 at 2:50pm
Nov 22, 2013 at 2:51pm
else (D < 0);
You have a stray terminator (;).
Nov 22, 2013 at 2:53pm
Line 30 else; remove the semicolon
Line 8, change int to double
Line 2, change <math.h> to <cmath>
Last edited on Nov 22, 2013 at 2:55pm
Nov 22, 2013 at 2:54pm
closed account (ihASLyTq)
Oh, thanks a lot! Now it's working fine. :)
Topic archived. No new replies allowed.