Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 8 of 8
Thread: C++ Help. CAN'T FIGURE IT OUT =(

10122007, 04:50 AM #1
 Join Date
 Feb 2007
 Posts
 21
 Thanks
 1
 Thanked 0 Times in 0 Posts
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; float quadraticFormula; float quadraticFormula2; system("cls"); cout << "Enter 1st Coefficient: "; cin >> a; cout << "Enter 2nd Coefficient: "; cin >> b; cout << "Enter 3rd Coefficient: "; cin >> c; if (a != 0 & b*b4*a*c > 0) { quadraticFormula = b + sqrt(b*b4*a*c) / (2* a); //Calculates the + of the zero quadraticFormula2 = b  sqrt(b*b4*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
Last edited by odinzu; 10122007 at 05:40 AM.
10122007, 05:16 AM
#2
 Join Date
 May 2006
 Location
 Ontario, Canada
 Posts
 392
 Thanks
 2
 Thanked 20 Times in 20 Posts
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.
Help us help you.
10122007, 05:40 AM
#3
 Join Date
 Feb 2007
 Posts
 21
 Thanks
 1
 Thanked 0 Times in 0 Posts
alright, updated. Is that ok?
10122007, 08:14 AM
#4
 Join Date
 Feb 2007
 Posts
 21
 Thanks
 1
 Thanked 0 Times in 0 Posts
I guess not =\
10122007, 06:13 PM
#5
 Join Date
 May 2006
 Location
 Ontario, Canada
 Posts
 392
 Thanks
 2
 Thanked 20 Times in 20 Posts
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.
10122007, 08:05 PM
#6
 Join Date
 Mar 2007
 Location
 Florida, USA
 Posts
 3,638
 Thanks
 2
 Thanked 404 Times in 396 Posts
That does not compile in VC++ due to the following:
 .h in the includes
 math include had to be changed to cmath
 missing namespace
 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.
10122007, 10:50 PM
#7
 Join Date
 Feb 2007
 Posts
 21
 Thanks
 1
 Thanked 0 Times in 0 Posts
10132007, 05:47 AM
#8
 Join Date
 May 2006
 Location
 Ontario, Canada
 Posts
 392
 Thanks
 2
 Thanked 20 Times in 20 Posts
Glad you got it working!