Complex Square Root

Hi,

I'm just wanting to write a program that requests the integers a, b and c from the user, calculates the roots and displays the result(s).

I get (nan, 0) on the output always. How do I properly deal with complex numbers?

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 <complex>

using namespace std;

int main()
{
    int a;
    int b;
    int c;
    complex<float> root1;
    complex<float> root2;
    char Status = 'Y';

    cout << "Input quadratic coefficient for ax^2 + bx + c = 0";

    while (Status == 'Y')
    {
        cout << endl << "a = ";
        cin >> a;
        cout << "b = ";
        cin >> b;
        cout << "c = ";
        cin >> c;

        root1 = (-b + sqrt((b^2) - 4*a*c))/(2*a);        // Take the complex sqrt
        root2 = (-b - sqrt((b^2) - 4*a*c))/(2*a);
        cout << endl << "root1 = " << root1 << endl;
        cout << "root2 = " << root2 << endl << endl;
        cout << "Try again? (Y = yes, Q = quit)";
        cin >> Status;
    }
    cout << "Program terminated." << endl;
}


Thanks :)
(b^2) - 4*a*c is not a complex number, so a sqrt for non complex numbers will be called. make a,b,c complex.
b^2 is not what you think it is. just do b*b
Ok but now I'm getting an error: "invalid suffix "a" on integer constant" Here is the new 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 <complex>

using namespace std;

int main()
{
    complex<float> a;
    complex<float> b;
    complex<float> c;
    complex<float> root1;
    complex<float> root2;
    char Status = 'Y';

    cout << "Input quadratic coefficient for ax^2 + bx + c = 0";

    while (Status == 'Y')
    {
        cout << endl << "a = ";
        cin >> a;
        cout << "b = ";
        cin >> b;
        cout << "c = ";
        cin >> c;

        root1 = (-b + sqrt((b*b) - 4*a*c))/(2*a);        // Take the complex sqrt
        root2 = (-b - sqrt((b*b) - 4*a*c))/(2*a);
        cout << endl << "root1 = " << root1 << endl;
        cout << "root2 = " << root2 << endl << endl;
        cout << "Try again? (Y = yes, Q = quit)";
        cin >> Status;
    }
    cout << "Program terminated." << endl;
}

This compiles fine for me. The error sounds like you tried compiling 2a instead of 2*a..
That is very strange. I copied the code from above and re-pasted in CodeBocks. Here it is again... I get....

In function 'int main()': ... error: no match for 'operator*' in '4 * a' ... error: no match for 'operator*' in '2 * a' ...


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 <complex>

using namespace std;

int main()
{
    complex<float> a;
    complex<float> b;
    complex<float> c;
    complex<float> root1;
    complex<float> root2;
    char Status = 'Y';

    cout << "Input quadratic coefficient for ax^2 + bx + c = 0";

    while (Status == 'Y')
    {
        cout << endl << "a = ";
        cin >> a;
        cout << "b = ";
        cin >> b;
        cout << "c = ";
        cin >> c;

        root1 = (-b + sqrt((b*b) - 4*a*c))/(2*a);        // Take the complex sqrt
        root2 = (-b - sqrt((b*b) - 4*a*c))/(2*a);
        cout << endl << "root1 = " << root1 << endl;
        cout << "root2 = " << root2 << endl << endl;
        cout << "Try again? (Y = yes, Q = quit)";
        cin >> Status;
    }
    cout << "Program terminated." << endl;
}
Last edited on
I use a different compiler, so I can't be sure, but try replacing 2 with float(2), same for 4
Last edited on
Topic archived. No new replies allowed.