Please help me with this, it is supposed to find all roots of a quadratic formula. I can't get it to work, what is wrong? :(

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

/*

----------------------------------------------------

This program is by Brandon Cavendish. 9/7/2017

It will find the real roots for a quadratic equation

if > 0

If == 0 (repeated)

if < 0

x1, x2 = -b+-sqrt(b^2-4ac)/2a

----------------------------------------------------

*/

// declaring variables.

double a = 0.0;

double b = 0.0;

double c = 0.0;

double x1 = 0.0;

double x2 = 0.0;

double realPart = 0.0;

double imaginaryPart = 0.0;

char Response;

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

cout << "Enter A:" << endl;

cin >> a;

cout << "Enter B:" << endl;

cin >> b;

cout << "Enter C:" << endl;

cin >> c;

if (discriminant > 0) {

x1 = (-b + sqrt(discriminant)) / (2*a);

x2 = (-b - sqrt(discriminant)) / (2*a);

cout << "Roots are real and different." << endl;

cout << "x1 = " << x1 << endl;

cout << "x2 = " << x2 << endl;

}

else if (h == 0) {

cout << "Roots are real and same." << endl;

x1 = (-b + sqrt(discriminant)) / (2*a);

cout << "x1 = x2 =" << x1 << endl;

}

else if (discriminant < 0) {

realPart = -b/(2*a);

imaginaryPart =sqrt(-discriminant)/(2*a);

cout << "Roots are complex and different." << endl;

cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;

cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;

}

cout << "Would you like to go again Y or N?" << endl;

cin >> Response;

while(Response == 'Y' || Response == 'y'){

cout << "Enter A:" << endl;

cin >> a;

cout << "Enter B:" << endl;

cin >> b;

cout << "Enter C:" << endl;

cin >> c;

if (discriminant > 0) {

x1 = (-b + sqrt(discriminant)) / (2*a);

x2 = (-b - sqrt(discriminant)) / (2*a);

cout << "Roots are real and different." << endl;

cout << "x1 = " << x1 << endl;

cout << "x2 = " << x2 << endl;

}

else if (discriminant == 0) {

cout << "Roots are real and same." << endl;

x1 = (-b + sqrt(discriminant)) / (2*a);

cout << "x1 = x2 =" << x1 << endl;

}

else if (discriminant < 0 ){

realPart = -b/(2*a);

imaginaryPart =sqrt(-discriminant)/(2*a);

cout << "Roots are complex and different." << endl;

cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;

cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;

}

cout << "Would you like to go again Y or N?" << endl;

cin >> Response;

}

cout << "The end" << endl;

return 0;

}

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

/*

----------------------------------------------------

This program is by Brandon Cavendish. 9/7/2017

It will find the real roots for a quadratic equation

if > 0

If == 0 (repeated)

if < 0

x1, x2 = -b+-sqrt(b^2-4ac)/2a

----------------------------------------------------

*/

// declaring variables.

double a = 0.0;

double b = 0.0;

double c = 0.0;

double x1 = 0.0;

double x2 = 0.0;

double realPart = 0.0;

double imaginaryPart = 0.0;

char Response;

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

cout << "Enter A:" << endl;

cin >> a;

cout << "Enter B:" << endl;

cin >> b;

cout << "Enter C:" << endl;

cin >> c;

if (discriminant > 0) {

x1 = (-b + sqrt(discriminant)) / (2*a);

x2 = (-b - sqrt(discriminant)) / (2*a);

cout << "Roots are real and different." << endl;

cout << "x1 = " << x1 << endl;

cout << "x2 = " << x2 << endl;

}

else if (h == 0) {

cout << "Roots are real and same." << endl;

x1 = (-b + sqrt(discriminant)) / (2*a);

cout << "x1 = x2 =" << x1 << endl;

}

else if (discriminant < 0) {

realPart = -b/(2*a);

imaginaryPart =sqrt(-discriminant)/(2*a);

cout << "Roots are complex and different." << endl;

cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;

cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;

}

cout << "Would you like to go again Y or N?" << endl;

cin >> Response;

while(Response == 'Y' || Response == 'y'){

cout << "Enter A:" << endl;

cin >> a;

cout << "Enter B:" << endl;

cin >> b;

cout << "Enter C:" << endl;

cin >> c;

if (discriminant > 0) {

x1 = (-b + sqrt(discriminant)) / (2*a);

x2 = (-b - sqrt(discriminant)) / (2*a);

cout << "Roots are real and different." << endl;

cout << "x1 = " << x1 << endl;

cout << "x2 = " << x2 << endl;

}

else if (discriminant == 0) {

cout << "Roots are real and same." << endl;

x1 = (-b + sqrt(discriminant)) / (2*a);

cout << "x1 = x2 =" << x1 << endl;

}

else if (discriminant < 0 ){

realPart = -b/(2*a);

imaginaryPart =sqrt(-discriminant)/(2*a);

cout << "Roots are complex and different." << endl;

cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;

cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;

}

cout << "Would you like to go again Y or N?" << endl;

cin >> Response;

}

cout << "The end" << endl;

return 0;

}

For one, it doesn't help that you calculate the discriminant before you get the input.

closed account (*48T7M4Gy*)

http://www.cplusplus.com/forum/general/222488/ DUPLICATE ALREADY BEING WORKED ON

closed account (*48T7M4Gy*)

@Brandon Stick with one and only one thread if its about the same program. Pick one that suits you and close all the others you have duplicated with a green tick.

Duplicates waste peoples time and effort and don't help your cause at all :)

Duplicates waste peoples time and effort and don't help your cause at all :)

Topic archived. No new replies allowed.