Need help with coding quadratic formula

I'm trying to code a program in C++ to: ask for 3 integers, display them, and depending on the numbers, use the if code to output results.

if the equation is quadratic (a is nonzero) and the roots are real (b2-4ac>=0), compute the root(s) and display them with 3 digits to the right of the decimal and labels

if the equation is not quadratic (a is zero), compute the root and display it with 3 digits to the right of the decimal and a label

if the equation is quadratic but the roots are complex (b2-4ac<0), then display a message stating that the roots are complex and will not be computed

This is my 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
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int a;                                                
    int b;                                                 
    int c;                                               
    double quadratic1;                            
    double quadratic2;
    double rootscheck = 0;
    cout << "Type three values." << endl;
    cin >> a >> b >> c ;
    cout<< a << b << c << endl;
    if (a != 0 && rootscheck >= 0)
      {
        quadratic1 = (-b + sqrt(b*b - 4*a*c))/2*a;
        quadratic2 = (-b - sqrt(b*b - 4*a*c))/2*a;
        cout << quadratic1 << quadratic2;
     }
    else
      {
        if (a == 0)
          {
            quadratic1 = (-b + sqrt(b*b - 4*a*c))/2*a;
            quadratic2 = (-b - sqrt(b*b - 4*a*c))/2*a;
            cout << quadratic1 << quadratic2;
          }
        else
            cout << "Roots are complex and will not be computed.";
      }
return 0;
}


It compiles, but only runs a small portion. When I input numbers, it does the " cout<< a << b << c << endl;" step, then stops. How can I make it run the rest?
what's rootscheck? Looks like it stays at value 0 and never changes
Looked at it a bit more.

1. (Minor) <cmath> instead of <math.h> ; doesn't matter too much since we're using all of namespace std.
2. Use setf and precision to set the decimal points to 3.
3. Use logical variable names to clearly depict what you're storing, like booleans for whether things are real.
4. You have a logic error with .../2*a . It should be .../(2*a) so that it divides by the product of 2 and a, instead of dividing by 2 and then multiplying by a.
5. Check for extra edge cases, like only one root even if quadratic (constants 1 2 1 for example)

Example 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
35
36
37
38
39
40
41
42
43
44
45
46
#include <cmath>
#include <iostream>

using namespace std;
int main()
{
    int a, b, c;
    bool is_quadratic, real_roots;
    double root1, root2;
    cout.setf(ios::fixed);
    cout.precision(3);
    
    cout << "Quadratic Formula.  ax^2 + bx + c = 0\n";
    cout << "Enter space-separated constants a b c:\n";
    cin >> a >> b >> c ;
    
    is_quadratic = a!=0;
    real_roots = b*b - 4*a*c >= 0.0;
    
    if (is_quadratic)
    {
      if (real_roots)
      {
        root1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
        root2 = (-b - sqrt(b*b - 4*a*c))/(2*a);
        
        cout << "\n\nRoot(s) are "<<root1;
        if (root1 != root2)
        {
          cout << " and "<< root2;
        }
        cout << endl;
      }
      else
      {
        cout << "Roots are complex and will not be computed.\n";
      }
    }
    else // Not quadratic
    {
      root1 = -c / (double)b;
      cout << "\n\nRoot is " << root1 << endl;
    }
    
    return 0;
}


Further checks can still be to improve robustness. What if user enters 0 0 2 ?

Can run it in online interpreter: https://repl.it/repls/BeautifulBlankDesigner
Last edited on
Thanks, I forgot to add it. It's meant to check whether the roots are real or complex, as stated in the program requirements.

I added "rootscheck = (b*b - 4*a*c);" just before the if block.

However, it does not output the correct results. Putting 2, -5, -3 has it give "12-2" as a result. Putting 0, 2, 5 has it give "0-0."
As I said, you had a logic error with your division by 2a. I have working code above ^_^
Thank you. The fixes worked perfectly.
Topic archived. No new replies allowed.