Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: C++ Help. CAN'T FIGURE IT OUT =(

1. ## C++ Help. CAN'T FIGURE IT OUT =(

I have been working on this simple program for hours now and still can't figure out my problem. Could someone please lend me some help? Thanks in advance.

Instructions:
Write a complete C++ program which will prompt the user for the three coefficients of a quadratic polynomial, and will then compute and display the two zeros of that polynomial. Use the quadratic formula.

Code:
```#include <iostream.h>
#include <math.h>                               //Find the 2 zeros using the quadratic formula.
void main (void)
{
float a = 0.0;
float b = 0.0;
float c = 0.0;

system("cls");

cout << "Enter 1st Coefficient: ";
cin >> a;
cout << "Enter 2nd Coefficient: ";
cin >> b;
cout << "Enter 3rd Coefficient: ";
cin >> c;

if (a != 0 & b*b-4*a*c > 0)

{
quadraticFormula = -b + sqrt(b*b-4*a*c) / (2* a); //Calculates the + of the zero
quadraticFormula2 = -b - sqrt(b*b-4*a*c) / (2 * a); //Calculates the - of the zero
cout << "First Zero (+): " << quadraticFormula << endl;
cout << "Second Zero (-): " << quadraticFormula2 << endl;

}
else

{
system("PAUSE");
cout << endl;
cout << "Error! Press Any Key to Terminate...";
}//end if

return;
} //end main```

• Rather than forcing people to guess at the purpose of your program and why it doesn't work it'd be helpful if you told us what you're trying to do, whether you're getting compile errors or runtime errors etc.

• alright, updated. Is that ok?

• I guess not =\

• You still haven't stated what the problem is. Is it not calculating properly? Is it not compiling? Is crashing when you run it?

At first glance, try checking your equations.
i.e. -b + sqrt((b*b)-(4*a*c)) / (2* a)
Some bracketing may help (like above) to make sure the order of computation is being done correctly.

• That does not compile in VC++ due to the following:
1. .h in the includes
2. math include had to be changed to cmath
3. missing namespace
4. missing second ampersand in if()

If you're compiling with GCC you might not need to do 1, or 2, though. Even after it compiles the formula doesn't seem to do anything, but I might not be entering numbers that you're going to be using. Follow Gox's suggestion for using parenthesis to be sure the equation is executed in the proper order.

• Originally Posted by Gox
You still haven't stated what the problem is. Is it not calculating properly? Is it not compiling? Is crashing when you run it?

At first glance, try checking your equations.
i.e. -b + sqrt((b*b)-(4*a*c)) / (2* a)
Some bracketing may help (like above) to make sure the order of computation is being done correctly.
It crashes when I run it. I think I have finally fixed it. The problem was my equation parenthesis and where I put my equation. I had to put it in the if statement because it would take the sqrt and give me a domain error to start off.

• Glad you got it working!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•