i tried my writing my first script today :)
it is the solving the quadratic equation script and i found it in an older C++ book i'm working through
i made some edits to the script since i'm using Dev C++ as my compiler
(had this problem but fixed it http://www.physicsforums.com/showthread.php?t=238616)

here is my code

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  double root1,	root2, a, b, c,	root;

  cout << "Enter the coefficients a, b, c: ";
  cin  >> a >> b >> c;

  root = sqrt (b*b - 4.0*a*c);
  root1 =	.5*(root - b) / a;
  root2 =	-.5*(root - b) / a;
  cout << "The solutions are: " << root1  << " and " << root2 << "\n";

  return(0);
}

the script complies, and it runs asking for a,b, and c like it should
BUT when i enter 3 numbers and hit return the program closes. i've tried entering the numbers in a bunch of different ways but the program always closes without solving the quadratic equation of a,b,c.

please tell me what am i doing wrong?

thank you!!

Try adding this

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double root1,	root2, a, b, c,	root;

	cout << "Enter the coefficients a, b, c: ";
	cin  >> a >> b >> c;

	root = sqrt (b*b - 4.0*a*c);
	root1 =	.5*(root - b) / a;
	root2 =	-.5*(root - b) / a;
	cout << "The solutions are: " << root1  << " and " << root2 << "\n";

	cin >> b;

	return(0);
}

your program works finely.But it throws floating point error if the value of b*b is less than 4.0*a*c,because sqrt are not meant for negative numbers.
Also use getch() at the end of the program before return(0),so that the output is displayed till you press a key.
getch() prototype-conio.h

No, do not use getch() . That is a non-standard C function (not even C++) in only a couple compilers. Use gerard's solution.

Try adding this

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double root1,	root2, a, b, c,	root;

	cout << "Enter the coefficients a, b, c: ";
	cin  >> a >> b >> c;

	root = sqrt (b*b - 4.0*a*c);
	root1 =	.5*(root - b) / a;
	root2 =	-.5*(root - b) / a;
	cout << "The solutions are: " << root1  << " and " << root2 << "\n";

	cin >> b;

	return(0);
}

thank you for your reply!
my program now lets me enter in 3 numbers and gives the solutions.

however, no matter what numbers i enter in the output is always "the solutions are: -1. #IND and -1. #IND.

what does that mean??

Thanks!!

The (b^2 - 4ac) portion of the equation is called the "discriminant". In order for the quadratic equation to solve properly, the discriminant must be greater than or equal to zero; it can not be negative. You should calculate it and determine if it's within the legal range before you attempt to take the square root of it.

You may want to look at this article.

Edited 5 Years Ago by Fbody: n/a

The (b^2 - 4ac) portion of the equation is called the "discriminant". In order for the quadratic equation to solve properly, the discriminant must be greater than or equal to zero; it can not be negative. You should calculate it and determine if it's within the legal range before you attempt to take the square root of it.

You may want to look at this article.

Ah, geez I feel so dumb. I knew this. I did the discriminant in my head and got it wrong lol.


Thank you all for your help!

This question has already been answered. Start a new discussion instead.