Code:
#include <iostream>
#include <math>
using namespace std;
// Same as yours
void getInputs( double& a, double& b, double& c )
{
cout<<"Please input coefficient a: ";
cin>>a;
cout<<"Please input coefficient b: ";
cin>>b;
cout<<"Please input the constant c: ";
cin>>c;
}
// I changed this to return a value which defines the type of root
// Imaginary, real or zero. Just copied from main
int quadraticSolver(double a, double b, double c, double& root1, double& root2)
{
if ((a!=0) && ((b*b-4*a*c)>0))
{
root1=(-b+sqrt(b*b-4*a*c))/2*a;
root2=(-b-sqrt(b*b-4*a*c))/2*a;
}
if ((a!=0) && ((b*b-4*a*c)==0))
{
root1=(-b)/(2*a);
}
int no_of_root = 0;
if ( (b*b-4*a*c)>0 )
no_of_root=2;
if ( (b*b-4*a*c)==0 )
no_of_root=1;
if ( (b*b-4*a*c)<0 )
no_of_root=0;
return no_of_root;
}
// Same as yours
void displayResult(double x1, double x2, int nr_roots)
{
switch(nr_roots)
{
case 0: cout<<"The root is imaginary."; break;
case 1: cout<<"The root is x = "<<x1; break;
case 2: cout<<"The roots are x = "<<x1<<" and x = "<<x2; break;
}
}
int main( void )
{
double a, b, c, r1, r2;
int no_of_root;
getInputs( a, b, c );
no_of_root = quadraticSolver( a, b, c, r1 ,r2 );
displayResult( r1, r2, no_of_root );
return 0;
}
Just made some changes. You also would have gotten errors about not having a, b, and c defined in main, and you using them. They weren't global, so you don't get access to them from functions.