Can't find what is wrong with this. Please help?
Jul 6, 2018 at 12:47am UTC
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 Jul 7, 2018 at 8:52pm UTC
Jul 6, 2018 at 1:01am UTC
"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.
Jul 6, 2018 at 2:42am UTC
@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.
Jul 6, 2018 at 2:48am UTC
@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
Jul 6, 2018 at 3:51am UTC
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 Jul 7, 2018 at 8:52pm UTC
Jul 6, 2018 at 4:40am UTC
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/
Jul 6, 2018 at 3:33pm UTC
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 Jul 6, 2018 at 3:35pm UTC
Jul 8, 2018 at 1:06am UTC
It worked!! Thank you guys a lot!
Topic archived. No new replies allowed.