C++ And Dreaded Imaginary Numbers...

Hey everyone. I'm trying to make a quadratic formula calculator where it will show the roots, the discriminant, vertex, etc...the issue is, I want the program to show imaginary numbers in the form of either "i" or SQRT(-1)....I've never learned how to do that, so I'm stumped.

Here's my code. Thanks everyone.

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
49
50
51
52
#include "quadCalc.h"
#include <iostream>
#include <cmath>
#include <complex>

using namespace std;

double discriminant (double disc1, double disc2, double disc3)
{
    cout << "Discriminant: " << endl;
    return (disc1*disc1) - 4 * disc2 * disc3;
}

double posQuad (double pos1, double pos2, double pos3)
{
    cout << "\nRoot 1: " << endl;
    return -pos1+sqrt((pos1*pos1)-4 * pos2 * pos3)/(2 * pos1);
}

double negQuad (double quad1, double quad2, double quad3)
{
    cout << "\nRoot 2: " << endl;
    return 0-quad1-sqrt((quad1*quad1)-4 * quad2 * quad3)/(2 * quad1);
}

void quadCalc () // ADD IMAGINARY NUMBER SUPPORT!!!!
{
    double a, b, c;
    bool loop = true;

    cout << "Quadratic Formula Calculator" << endl;
    do {

        do {

    cout << "\nA: "; cin >> a;
    cout << "B: "; cin >> b;
    cout << "C: "; cin >> c;

    if (a == 0)
    {
        cout << "A cannot equal zero. Please re-input!" << endl;
    }

    } while (a == 0);

    cout << discriminant (a,b,c);
    cout <<  posQuad (a,b,c);
    cout << negQuad (a,b,c);

    } while (loop == true);
}
Topic archived. No new replies allowed.