SOURCE FILE: quadrat_v3.cpp



/*
    This is a program to solve 
    a series of quadratic equations like 5X^2 + 6X + 1 = 0.
*/

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

int main (void)
{
   double x1, x2, discr, sqrtDiscr, a, b, c ;
   char answer  ;

   do
   {
      cout << "\nPlease enter a real number for coefficient a: " ;
      cin >> a ;
      cout << "Please enter a real number for coefficient b: " ;
      cin >> b ;
      cout << "Please enter a real number for coefficient c: " ;
      cin >> c ;
   
      discr = b*b - 4*a*c ;

      if ( discr < 0 )
      {  // Do this if discr < 0
         cout << "\nSorry, no real solution.\n" ;
      }
      else
      {  // Do this if discr >= 0
         if (discr == 0)
         {
              // Do this if discr ==  0
            cout << "\nThere is only one root: " << ( -b/(2*a) ) << endl ;
         }
         else 
         {    // Do this if discr > 0
            sqrtDiscr = sqrt(b*b - 4*a*c) ;
            x1 = (-b + sqrtDiscr) / (2*a) ;
            x2 = (-b - sqrtDiscr) / (2*a) ;
            cout << "\nThe first root is: " << x1 << endl ;
            cout << "The second root is: " << x2 << endl ;
         }
      }
      cout << endl ; // Print a blank line after each solution
      cout << "Do you want to do another problem? (y/n): " ;
      cin >> answer ;
   } while ( answer == 'y' ) ;

   cout << endl ;
   return 0 ;
}