To quickly explain, I need six different possible cout outputs. Three for when A = 0 which don't work, and then three more for when it doesn't equal 0 which work flawlessly. I think I know my problem, the set of if statements under '//A DOES equal 0', can never run. What's wrong with my second set of nested if statements?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double numA = 0;
double numB = 0;
double numC = 0;
int rootOne = 0;
int rootTwo = 0;
// User inputs roots
cout << "Program computes and prints the real roots of a quadratic polynomial a*x^2 + b*x + c." << endl;
cout << "Enter three real numbers a, b, c, seperated by spaces:";
cin >> numA >> numB >> numC;
//Calculating the Roots; (-b + sqrt(b * b - 4 * a * c)) / (2 *a) and (-b - sqrt(b * b - 4 * a * c)) / (2 *a)
rootOne = (-numB + sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);
rootTwo = (-numB - sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);
// A doesn't equal 0
if (numA =! 0) {
if ((numB * numB) - (4 * numA * numC) > 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two distinct real roots" << endl;
cout << "root 1 = " << rootOne << " root2 = " << rootTwo << endl;
}else if ((numB * numB) - (4 * numA * numC) == 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two equal real roots" << endl;
cout << "root 1 = root2 = " << rootTwo << endl;
}else if ((numB * numB) - (4 * numA * numC) < 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two complex roots" << endl;
}
}
//A does equal 0
if (numA == 0){
if ((numB == 0) && (numC =! 0)){
cout << "No roots for the constand function of " << numC << endl;
}else if ((numB == 0) && (numC == 0)){
cout << "No roots for the degenerate case of 0 = 0." << endl;
}else{
cout << "The only root for the linear case of " << numA << "*x^2 + " << numB << "*x + " << numC << "is: " << numB << endl;
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire