So I managed to get everything working right except for one thing, when I enter the following input:

a = 2
b = 2
c = 2

I should be getting this:

Root 1 = -0.5 + 0.866025i
Root 2 = -0.5 - 0.866025i

But I get this:

Root 1 = -1.#IND
Root 2 = -1.#IND

I'm at a loss here trying to figure out how to get the output to display like it should, hope you guys can help.

#include <iostream>
using namespace std;
#include <stdlib.h>

int main()
{
	double a;
	double b;
	double c;

	cout << "Enter number for a: "; 
		cin >> a;

	cout << "Enter number for b: "; 
		cin >> b;

	cout << "Enter number for c: "; 
		cin >> c;

	if (a == 0 || b == 0 || c == 0) 
	{
	  cerr << "0 is an invalid input" << endl;
	}

	else   
	{
	   double root_1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); 
	   double root_2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);

	   cout << "Root 1: " << root_1 << endl; 
	   cout << "Root 2: " << root_2 << endl << endl;
	}

	return 0;
}

Your problems include two things that come to mine:

(a) the answer that you require is complex, but you are trying to put that into a double. Not going to work. Instead add a #include <complex> to your headers and use std::complex<double> root; . You are also going to have to be careful with your sqrt. Since the incorrect sqrt function (that taking a double) will be called if you don't. [i.e. you want to cast the b*b-4*a*c to a complex before taking the sqrt. ]

(b) Your test for zero is strange, what is wrong with this quadratic x^2-9=0 ??
[Small reminder, testing doubles as a==0 is often not going to go well, as you are likely to have a very small number not zero]. Anyway, you only have to check is a is not too small to cause an overflow. That is a better test done after the calculation.

How would I use the std::complex<double> root;? I just started using C++ this semester so I don't know too much.

you don't have to use complex numbers if you don't want to. Define the equation using variables of type double and check if 4*a*c is less than b*b before running solving the full equation. If that relationship doesn't hold, then notify user that the equation can not be solved using type double alone and move on.

Likewise, you don't need to check if b and c are zero. If either is zero the equation just simplifies. However, if a is zero, then you get a divide by zero, which is not defined.

Having looked at Rebellions test case, which is clearly has complex roots, I think that you are going to have to use complex numbers or use two doubles. Since two doubles is a bit ugly let me talk about std::complex<double>.

First of there is an amazing amount of stuff in that type, especially for the beginner. There is a namespace (std) and there is a template class. Both of which deserver a whole chapter in Stroustrup's C++ book!

However, the beauty of C++ is that once you OR someone else, has actually defined a class, complete with all the operators (e.g. +,-,* etc), you can use it almost as if it is a normal type, without worrying about exactly what it is.

So if you did this

// Note: have to convert b*b-4ac to a complex or sqrt(double) will be called.
std::complex<double> sPart=sqrt(std::complex<double>(b*b-4*a*c));
and 
std::complex<double> root1= (-b - sPart)/(2*a); 
std::complex<double> root2= (-b + sPart)/(2*a);

You could then just output your result. e.g. std::cout<<"Root 1 == "<<root1<<std::endl; , or you can check that the solution is a complement
by doing root1*root2, etc.

This may not be what you want, however, you can also solve your problem by noticing that if b*b-4*a*c is less than 0, then that part is the imaginary value, and you can
output it as std::cout<<"Number = "<<-b/(2*a)<<" "<<sqrt(4*a*c-b*b)/(2*a)<<"j"<<std::endl; ;

Finally, If a is zero there is still a solution, since you have b*x+c=0 , i.e x = -c/b.

Lastly, being able to write a complex template class will take a reasonable amount of learning + work. However, to use a class takes a lot less. This is one of the original fundamental goals of C++.

I agree completely with StuXY. It's really a matter of whether you want to use a header/library you haven't used before (or if this is a class assignment, then whether you are allowed to use classes/headers/libraries you haven't been introduced to in class yet).

Alright sweet i got it working! thank you guys!

hi i want the user enter the Quadratic equation like that (ax^2+bx+c)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.