having problems with my recursion statements

The code below is supposed to prompt the user for 3 coefficients and give back whether the roots have one, two, or no real solutions and give the actual number as well. the code compiles but every time i enter numbers that should give me two real roots, it also prints the imaginary roots too. Im not sure where i messed up with my recursion statements but I would really appreciate it if someone helped point it out to me.
Thanks in advance!

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  // This program is intended to be a fully functional root calculator.

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <ctime>

using namespace std;

//function prototypes
double quadraticpositive(double a,double b,double c);
double quadraticnegative(double a,double b,double c);
double quadraticzero(double a, double b);
double discriminant(double a,double b,double c);
bool Runagain (void);


//main function
int main()
{
    cout << "This program can calculate the roots" << endl;
    cout << "of an equation using the quadratic forumla." << endl;
    cout << endl;
    cout << endl;

do {

    double a, b, c;
    cout << "Enter coefficient a:"<< endl;
    cin >> a;
    cout << "Enter coefficient b:" << endl;
    cin >> b;
    cout << "Enter coefficient c:" << endl;
    cin >> c;
    cout << endl;
    cout << endl;


    if (discriminant (a, b, c) > 0 )
		{
        cout << "There are two real solutions." << endl;
		cout << endl;
		cout << "Equation: " << a << "x^2 + " << b << "x - " << c << endl;
		cout << endl;
		cout << "Has Roots: x = " << quadraticpositive(a,b,c) << " and x = " << quadraticnegative(a,b,c) << endl;
		cout << endl;
		}

	else if (discriminant (a, b, c) == 0 )
		{
        cout << "There is one real solution." << endl;
        cout << endl;
		cout << "Equation: " << a << "x^2 + " << b << "x - " << c << endl;
		cout << endl;
		cout << "Has Root: x = " << quadraticzero(a,b) << endl;
		cout << endl;
		}
    else (discriminant (a, b, c) < 0 );
		{
        cout << "There are two complex soultions." << endl;
        cout << endl;
		cout << "Equation: " << a << "x^2 + " << b << "x - " << c << endl;
		cout << endl;
		cout << "Has Roots: x = " << b << " + " << discriminant (a,b,c) << "i and x = " << b << discriminant (a,b,c) << "i" << endl;
		cout << endl;
		}
}

    while (Runagain());

    return(0);

}

//function implementation

bool Runagain(void)
{

	char userResponse;

	cout << "\nWould you like to run again (y or n): ";
	cin >> userResponse;

	if(userResponse == 'y')
		return(true);

	return(false);
}

double discriminant (double a, double b, double c)
{
	double d;
	d = (sqrt((b*b)+(-4*a*c)));
		return (d);
}

double quadraticpositive(double a,double b,double c)
{
    double qp;
    qp = ((-(b) + (discriminant (a,b,c)))/(2*a));
    return (qp);

}

double quadraticnegative(double a,double b,double c)
{
    double qn;
    qn = ((-(b) - ((discriminant (a,b,c))))/(2*a));
    return (qn);
}

double quadraticzero(double a, double b)
{
    double qz;
    qz = (-(b)/(2*a));
    return (qz);
}
Replace line 58 else (discriminant (a, b, c) < 0 ); with just else. My guess is that you wanted else if (discriminant (a, b, c) < 0 ).Note, no semicolon at the end of the statement. . If you have the semicolon, else would refer to the statement just until the end of that line, and everything after that is always executed. Also note that you don't need to check if the discriminant is negative, since if it is not positive or 0, then it necessarily is less then 0.
Ah, that makes so much sense. Thank you! I swear i was staring at that code for an hour trying to figure out what went wrong.
Topic archived. No new replies allowed.