//*************************************************************************
//  Name:            David Wills
//  Program:         quad2.cpp                   Version 2
//  Compiler:        g++
//  Date:            28 Oct. 1999
//  Location:        Kadena Air Base
//                                  
// Modified: 9/25/00 DFW  misc.
//
//  This program computes the quadratic formula.  It asks the user
//  to enter the three coefficients of a quadratic expression.
//  The two possible solutions for the variable are determined
//*************************************************************************


#include <iostream>
#include <cmath>                  //sqrt
#include <cstdlib>               //EXIT_SUCCESS

using namespace std;

int main () {
  float a, b, c, discriminant, x1, x2;
  char again;

  again = 'y';
  while (again == 'y') {
    cout << "Enter a: ";
    cin >> a;
    while (a == 0) {       //ensure that a is not zero
      cout << "ERROR.  a can not be zero.  Re-enter a: ";
      cin >> a;
    }
    cout << "Enter b and c: ";
    cin >> b >> c;

    discriminant = b*b - 4*a*c;

    if (discriminant > 0) {
      // calculate the two possible real values for x.
      x1 = (-b + sqrt(discriminant)) / (2 * a);
      x2 = (-b - sqrt(discriminant)) / (2 * a);

      cout << "x1: " << x1 << "    x2: " << x2 << endl;
    }
    else if (discriminant == 0) {
      //one real root
      x1 = -b / (2*a);
      cout << "x= " << x1 << endl;
    }
    else
      //no real roots
      cout << "Discriminant is negative." << endl;

    cout << endl << "Again? (y or n): ";
    cin >> again;
  }
  return EXIT_SUCCESS;
}



