The terminology is wrong. You are finding solutions to a quadratic
equation, either using the quadratic formula or completing the square. You are not "solving the quadratic formula by completing the square" (although the quadratic formula is derived by completing the square on the general equation).
Don't include "windows.h" if you aren't using anything from it.
Don't include "cmath" and "math.h" at the same time. "cmath" is the preferred header.
You should include <cstdlib> if you are using anything from it (such as system()).
If possible, don't clear the screen or pause using getch since these are both non-portable (and if you can omit them you can omit cstdlib and conio.h).
Don't use global variables.
Don't use goto.
Don't call main recursively.
Divide your program into functions.
Instead of if (sel > '3') you should use the default switch case, which will handle sel < '1', too.
It is of course possible that there is no "real" solution (if the determinant, b * b - 4 * a * c, is negative).
Here's how the basic structure of your program might look.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <cmath>
using namespace std;
void get_inputs(double& a, double& b, double& c)
{
//...
}
void show_equation(double a, double b, double c)
{
//...
}
void quadratic_formula()
{
cout << "\nSolving by using the quadratic formula\n\n";
double a, b, c;
get_inputs(a, b, c);
show_equation(a, b, c);
//...
}
void completing_the_square()
{
cout << "\nSolving by completing the square\n\n";
double a, b, c;
get_inputs(a, b, c);
show_equation(a, b, c);
//...
}
int main(){
while (true)
{
cout << "Solving a Quadratic Equation\n";
cout << " 1) by the quadratic formula\n";
cout << " 2) by completing the square\n";
cout << " 3) quit\n";
cout << "Choice: ";
char sel;
cin >> sel;
switch (sel){
case '1':
quadratic_formula();
break;
case '2':
completing_the_square();
break;
case '3':
cout << "Quitting.\n";
return 0;
default:
cout << "\n\nIncorrect choice. Try again.\n\n";
}
}
}
| |