Why doesn't this program function correctly?

I decided to write a program that solves ax^2+bx+c=0, but for some reason it just returns 0; or jumps to the last "else" and says "invalid equation"

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

int main ()
{
    cout << "Enter a for the equation ax^2+bx+c" << endl;
    double a,b,c;
    cin >> a;
    cout << "Enter b for the equation ax^2+bx+c" << endl;
    cin >> b;
    cout << "Enter c for the equation ax^2+bx+c" << endl;
    cin >> c;
    double A=a*2;
    double D=(b*b)-(4*a*c);
    if ( D > 0)
    {
        double x1,x2,sD;
        sD = sqrt (D);
        x1= (-b+sD)/A;
        x2= (-b-sD)/A;
        cout << "X1 equals " << x1 <<" and X2 equals " << x2 << endl;
          system("pause");
          return EXIT_SUCCESS;


    }
    else if (D=0)
    {
        double x1;
        x1= (-b)/A;
        cout << "X equals " << x1 << endl;
          system("pause");
          return EXIT_SUCCESS;
    }

    else if (D<0)
    {
        cout << "Invalid equation." << endl;
          system("pause");
          return EXIT_SUCCESS;
    }

}

else if (D=0)
= is an assignment operator.
== is a comparison operator. You want to use this one.

Also the code works fine anyways. Im not sure if the calculations are right since Im not gonna check that, but if you input a = 2, b = 15, c = 2, it enters the first if statement.
Last edited on
Thanks! I got it working!
Hi,

Be careful with equality with doubles: else if (D== 0.0) will almost certainly be false, because of the way doubles are stored.

To check for equality to zero, see if the absolute value is less than some arbitrary precision value:

1
2
3
4
5
6
7
const double PRECISION = 0.001;

const double a =  1.0 - 10.0 * 0.1 ; // not equal to zero, maybe +/-3e-16

if (std::abs(a) < PRECISION ) { // near enough to zero for us
  // do stuff
}


I always put the decimal point and at least 1 figure after the point, so it is obviously a double.

Good Luck !!
Last edited on
Translation for a beginner:

Be careful with equality with doubles: else if (D== 0.0) will almost certainly be false, because of the way doubles are stored.

Be careful when comparing double types. The computer will store very precise values. (eg 0.000012 != 0.0 even though in some cases it would be near enough to 0 that you'd prefer it to be considered 0).


To check for equality to zero, see if the absolute value is less than some arbitrary precision value:

One way to do this is set a constant value that is the closest value you don't want considered to be zero. (refer the example TheIdeasMan provided)


I always put the decimal point and at least 1 figure after the point, so it is obviously a double.

To make reading your source code easier later on down the road, or when someone else might need to read your code, it's a good practice to always add a decimal point, even when it's not explicitly required, to double and float types.
Just to be pedantic, I put figures before and after the decimal as well as in 1.0 not 1. or 0.3 not .3

The error in floating point types (float or double) is usually in the last decimal place which that type can handle, so 3e-16 for double say, and 2e-6 say for float .
Topic archived. No new replies allowed.