Solutions to Quadratic Formula

So, I need help for my plate 2 in school, I think my code was wrong because it didnt give the correct answer for factoring and maybe in completing the square too.

so my code goes like this, im open for more ideas and corrections, thanks you
this is the full statement of my work ibb.co/TBy6xr1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include<iostream>
#include<windows.h>
#include<cmath>
#include<conio.h>
#include<math.h>

using namespace std;

double a, b, c, x, y, p, d, ic, sat;
char sel;

int main(){
	
	system("cls");
	cout<<"\nSolutions to Quadratic Formula";
	cout<<"\n   Cases:";
	cout<<"\n\t 1.) Factoring ";
	cout<<"\n\t 2.) Completing the Square";
	cout<<"\n\t 3.) Cancel";
	sat:
	cout<<"\n   Choice : ";
	cin>>sel;
	
	switch (sel){
		case '1':
			system("cls");
			cout<<"\n Quadratic Formula by Factoring";
			cout<<"\n\t   Inputs  A : ";
			cin>>a;
			cout<<"\n\t           B : ";
			cin>>b;
			cout<<"\n\t           C : ";
			cin>>c;
			cout<<"\n   Equation: ";
			if ((a>0)&&(b>0)&&(c>0)){
				cout<<"\n\t "<<a<<"x^2 + "<<b<<"x + "<<c<<" = 0";
			}
			else if ((a<0)&&(b<0)&&(c<0)){
				cout<<"\n\t -"<<a<<"x^2 - "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b<0)&&(c<0)){
				cout<<"\n\t  "<<a<<"x^2 - "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b<0)&&(c>0)){
				cout<<"\n\t -"<<a<<"x^2 - "<<abs(b)<<"x + "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b>0)&&(c>0)){
				cout<<"\n\t -"<<a<<"x^2 + "<<abs(b)<<"x + "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b>0)&&(c<0)){
				cout<<"\n\t  "<<a<<"x^2 + "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b>0)&&(c<0)){
				cout<<"\n\t -"<<a<<"x^2 + "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b<0)&&(c>0)){
				cout<<"\n\t  "<<a<<"x^2 - "<<abs(b)<<"x + "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b==0)&&(c<0)){
				cout<<"\n\t -"<<a<<"x^2"<<"- "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b==0)&&(c>0)){
				cout<<"\n\t  "<<a<<"x^2"<<"+ "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b==0)&&(c>0)){
				cout<<"\n\t -"<<a<<"x^2"<<"+ "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b==0)&&(c<0)){
				cout<<"\n\t  "<<a<<"x^2"<<"- "<<abs(c)<<" = 0";
			}
			
			
			x = ((-1*b)+(sqrt((b*b)-(4*a*c))))/(2*a);
			y = ((-1*b)-(sqrt((b*b)-(4*a*c))))/(2*a);
			
			if ((x>0)&&(y>0)){
				cout<<"\n   Factors: ";
				cout<<"\n\t (x - "<<abs(x)<<") (x - "<<abs(y)<<")";	
			}
			else if((x<0)&&(y>0)){
				cout<<"\n   Factors: ";
				cout<<"\n\t (x + "<<abs(x)<<") (x - "<<abs(y)<<")";	
			}
			else if((x<0)&&(y<0)){
				cout<<"\n   Factors: ";
				cout<<"\n\t (x + "<<abs(x)<<") (x + "<<abs(y)<<")";	
			}
			else {
				cout<<"\n   Factors: ";
				cout<<"\n\t (x - "<<abs(x)<<") (x + "<<abs(y)<<")";	
			}
			
			
			if (x=y){
				cout<<"\n   Roots: ";
				cout<<"\n\t X1 = "<<x;
			}
			else {
				cout<<"\n   Roots: ";
				cout<<"\n\t X1 = "<<x;
				cout<<"\n\t X2 = "<<y;
			}
			
			getch();
			main();

			break;
		
	
		case '2':
			system("cls");
			cout<<"\n Quadratic Formula by Completing the Square";
			cout<<"\n\t   Inputs  A : ";
			cin>>a;
			cout<<"\n\t           B : ";
			cin>>b;
			cout<<"\n\t           C : ";
			cin>>c;
			cout<<"\n   Equation: ";
			if ((a>0)&&(b>0)&&(c>0)){
				cout<<"\n\t "<<a<<"x^2 + "<<b<<"x + "<<c<<" = 0";
			}
			else if ((a<0)&&(b<0)&&(c<0)){
				cout<<"\n\t -"<<a<<"x^2 - "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b<0)&&(c<0)){
				cout<<"\n\t  "<<a<<"x^2 - "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b<0)&&(c>0)){
				cout<<"\n\t -"<<a<<"x^2 - "<<abs(b)<<"x + "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b>0)&&(c>0)){
				cout<<"\n\t -"<<a<<"x^2 + "<<abs(b)<<"x + "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b>0)&&(c<0)){
				cout<<"\n\t  "<<a<<"x^2 + "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a<0)&&(b>0)&&(c<0)){
				cout<<"\n\t -"<<a<<"x^2 + "<<abs(b)<<"x - "<<abs(c)<<" = 0";
			}
			else if ((a>0)&&(b<0)&&(c>0)){
				cout<<"\n\t  "<<a<<"x^2 - "<<abs(b)<<"x + "<<abs(c)<<" = 0";
			}
			
			p = pow(a,.5);
			d = pow((b/(2*p)),2);
			ic = c*-1;
			x = (pow(ic,.5)) + (-1*d);
				
			if (x>0){
				cout<<"\n   Factors: ";
				cout<<"\n\t (x - "<<abs(x)<<")^2 = ";	
			}
			else{
				cout<<"\n   Factors: ";
				cout<<"\n\t (x + "<<abs(x)<<")^2 = ";	
			}
			cout<<"\n   Roots: ";
			cout<<"\n\t X1 = "<<x;
			
			getch();
			main();
			break;
	
		case '3':
			break;
}
	if (sel>'3'){
	
		cout<<"\n\n ENTRY DOESN'T EXIST. PLEASE CHECK YOUR ENTRY. Choose in the given above.";
		goto sat;
		}
	
}


Given a second degree polynomial ax^2 + bx + c, its factored form is a(x - x0)(x - x1), where x0 and x1 are its roots. You forgot the a factor. Also on line 94 you used = instead of == for the comparison.
There may be other problems with the code.
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";
        }
    }
}

Why is dev-c++ so popular???

kimmaylovesyou did you have to use dev-c++ for a course or in school?
Last edited on
Topic archived. No new replies allowed.