Can't find what is wrong with this. Please help?

When I try to compile it, it tells me something is wrong. I don't know what I am missing. It would be great if someone would help me. The program is supposed to solve for the discriminant and the quadratic 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
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
/*
This program will allow you to input a quadratic 
equation in its standard form, while outputting
the discriminant, roots and what type of root it is.
*/

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()

{
	// DECLARE VARIABLES
	float b, i;
	float a, b, c;
	float des;
	float root1, root2;	
	//INTRO
	cout << "To use this program you will need the standard form of a quadratic formula." << endl;
	cout << "The formula should look like this ax^2 + bx + c = 0. " << endl;
	cout << " " << endl;
	// Enter values
	cout << "Enter the 'a' value" << endl;
	cin >> a;
	cout << "Enter the 'b' value" << endl;
	cin >> b;
	cout << "Enter the 'c' value." << endl;
	cin >> c;
	cout << "" << endl;

	//Descriminant Calculation
	des = pow(b, 2) - (4 * a * c);
	cout << "The descriminant is: " << des << endl;
	cout << "" << endl;

// Roots
	///Squared (Rational/Irrational)
		double o_sqrt = sqrt(root1);
		int i_sqrt = o_sqrt;

		double t_sqrt = sqrt(root2);
		int i_sqrt = t_sqrt;

	if (des > 0)
	{
		root1 = (-b + sqrt(des)) / (2 * a);
		root2 = (-b - sqrt(des)) / (2 * a);
		//Irrational or Rational
		if ((o_sqrt == i_sqrt) && (t_sqrt == i_sqrt))
		{
		cout << "The two rational roots are:" << endl;
		}
		else
			{
		cout << "The two irrational roots are:" << endl;
		}
		cout << "x1 = " << root1 << ", x2 = " << root2 << endl;
	}

	else if (des = 0)
	{
		root1 = (-b + sqrt(des)) / (2 * a);
		cout << "The real and rational root is:" << endl;
		cout << "x1 & x2 = " << root1 << endl;
	}
	
	else
	{
		b = -b / (2 * a);
		i = sqrt(des) / (2 * a);
		cout << "The two complex, imaginary roots: " << endl;
		cout << "x1 = " << b << " + " << i << "i" << endl;
		cout << "x2 = " << b << " - " << i << "i" << endl;
	}


	cout << "" << endl;
		system("PAUSE");


			return (0);

}
Last edited on
"Something is wrong" isn't very good error reporting.

error: redeclaration of 'float b'
error: redeclaration of 'int i_sqrt'

1
2
float b, i;
float a, b, c; 

You can't have multiple variables with the same name.
@Hippogriff

OOHHHH! Now I see where it tells you where the errors are, sorry I'm new at this. Sorry for being a bother. It seems kind of stupid now.
@BGA6444

Another problem I see, is..
1
2
3
4
5
6
7
// Roots
///Squared (Rational/Irrational)
double o_sqrt = sqrt(root1); // root1 was NOT assigned a value
int i_sqrt = o_sqrt;

double t_sqrt = sqrt(root2);  // root2 also was NOT assigned a value
int i_sqrt = t_sqrt;


And this
1
2
else if (des = 0) // single equal sign, ASSIGNS a value, but a double equal (==)
                         // checks the variable against a value. You need the double equal here  
I got another problem. But the program compiles.
The problem is in this area:

1
2
3
4
5
6
7
8
else
{
b = -b / (2 * a);
i = sqrt(des) / (2 * a);
cout << "The two complex, imaginary roots: " << endl;
cout << "x1 = " << b << " + " << i << "i" << endl;
cout << "x2 = " << b << " - " << i << "i" << endl;
}


Instead of outputting the two cout lines it outputs this:

x1 = -2 + -nand(ind)i
x2 = -2 + -nand(ind)i

Do you know what may be causing the -nand(ind)?



Last edited on
closed account (E0p9LyTq)
NaN (Not A Number) likely means you are trying to take the square root of a negative number.

Learning to use code tags will make reading your source code MUCH easier.

HINT: you can edit your posts and add code tags.

http://www.cplusplus.com/articles/jEywvCM9/
To get the imaginary square root of a negative number will have to do something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;


int main() {
	
	int a = -25;

	cout << "The imaginary square root of -25 is ";

	a = sqrt(abs(a)) * -1;

	cout << a << "i.\n";


	return 0;
}
Last edited on
It worked!! Thank you guys a lot!
Topic archived. No new replies allowed.