1. ## Problem with Quad equation solver program

hey guys..I'm new here,and I need some help in this program I'm making..

Code:
```//Program to show the nature of roots,solve,etcetc
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include <cmath>
#include <conio.h>

using namespace std;
void setcolor(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}

int main()
{
float a,x,b,c,alpha,beta,D;
int y;
alpha = (-b - sqrt(pow(b,2) - 4*a*c))/2*a ;
beta = (-b + sqrt(pow(b,2) - 4*a*c))/2*a ;
D = pow(b,2) - 4*a*c;

cout << "\nWelcome to the Equation Solver!\n";
cout << "\nChoose one of the following -:\n";
cout << "\n1. Find a quadratic equation's nature of roots\n";
cout << "\n2. Find a quadratic equation's roots\n";
cout << "\n3. Establish the Vi&#232;te relation of the roots and coefficients\n";
cout << "\n4. Quit,noobs >_>\n";
cin >> y;

switch(y)
{
case 1:
cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..\n";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
setcolor(8);
if (D = 0)
{
cout << "The roots of this equation are real and equal!";
}
if (D > 0)
{
cout << "The roots of this equation are real and unequal!";
}
if (D < 0)
{
cout << "There are no roots to this equation..";
}
setcolor(7);
system("PAUSE");
break;
case 2:
cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
cout << "And the solutions are as follows -:\n";
setcolor(14);
cout << "x = " << alpha << endl;
cout << "x = " << beta << endl;
setcolor(7);
system("PAUSE");
break;
case 3:
cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
setcolor(8);
cout << "The sum of the roots is " << alpha + beta << endl;
cout << "The value of -b/a is " << -b/a << endl;
cout << "The product of the roots is " << alpha * beta << endl;
cout << "The value of c/a is " << c/a << endl;
setcolor(7);
system("PAUSE");
break;
case 4: break;
default: "Enter a number in the list\n";break;
}

return 0;
}```
Can't understand..there are no syntax errors,however there are logical ones.
In case 1, the Discriminant outputs(The ones with D) don't come.
In case 2, the equations always solve to some retarded value(lol?)
Case 3 doesn't work at all...

2. > alpha = (-b - sqrt(pow(b,2) - 4*a*c))/2*a
> beta = (-b + sqrt(pow(b,2) - 4*a*c))/2*a ;
> D = pow(b,2) - 4*a*c;
These need to be performed AFTER you have input the values, not before.

Also, you need to check that
pow(b,2) - 4*a*c
isn't negative before trying to get the square root of it.

3. I'd replace the...
Code:
`if(D=0)`
...with a...
Code:
`if(D==0)`
...if I were you. Single '=' means assigning a value, double '==' means a check statement @_@

4. You can get rid of pow(). Just use b*b instead of pow(b, 2). Also, the 2*a in the denominator needs to be in parentheses, otherwise it gets interpreted as "divide by 2, then multiply by a" instead of "divide by 2*a" as you intend.

5. Code:
```switch(y)
{
case 1:
cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..\n";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
D = pow(b,2) - (4*a*c);
setcolor(8);
if (D == 0)
{
cout << "The roots of this equation are real and equal!";
}
if (D > 0)
{
cout << "The roots of this equation are real and unequal!";
}
if (D < 0)
{
cout << "There are no roots to this equation..";
}
setcolor(7);
system("PAUSE");
break;
case 2:
cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
cout << "And the solutions are as follows -:\n";
setcolor(14);
D = pow(b,2) - (4*a*c);
alpha = (-b - sqrt(D))/2*a ;
beta =  (-b + sqrt(D))/2*a ;
setcolor(8);
if (D < 0)
{
cout << "This equation has no roots!";
}
else
cout << "x = " << alpha << endl;
cout << "x = " << beta << endl;
setcolor(7);
system("PAUSE");
break;
case 3:
cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
D = pow(b,2) - (4*a*c);
alpha = (-b - sqrt(D))/2*a ;
beta =  (-b + sqrt(D))/2*a ;
setcolor(8);
if (D < 0)
{
cout << "This equation has no roots!";
}
else
cout << "The sum of the roots is " << alpha + beta << endl;
cout << "The value of -b/a is " << -b/a << endl;
cout << "The product of the roots is " << alpha * beta << endl;
cout << "The value of c/a is " << c/a << endl;
setcolor(7);
system("PAUSE");
break;
case 4: break;
default: "Enter a number in the list\n";break;
}```
This is it now. The roots come in decimals..is there a way to show them in rational(fraction) or whole numbers instead? And thanks for the previous help..

6. Ah thanks..the parantheses thingy worked..the roots are coming correct now!

7. A few things:

Code:
```You have a lot of duplicate code there. You should separate it into functions.

Else requires a {} block after it. Other wise only the next line is considered part of the else condition.

Use descriptive variable names. y and D are not descriptive names. Neither are alpha and beta because they do not a person reading the code what the variable actually does. a, b, and c, are iffy as well.```
This is it now. The roots come in decimals..is there a way to show them in rational(fraction) or whole numbers instead? And thanks for the previous help..
You can round the fraction to a whole number. But there is no easy way to print out a fraction. You would have to write your own fraction class to do it.

8. Hmm thanks a lot...a fraction class omg..@_@ might take a while..

9. Most square roots cannot be represented by a fraction. To estimate them using fractions would be misleading, since the presence of whole numbers would seem to indicate perfect precision.

10. Thanks to ultrabot90,I was reminded of using classes...so I remade the crap, and added two more sections. Here it is...
Code:
```#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include <cmath>
#include <conio.h>

using namespace std;
void setcolor(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}

class Program
{
private:
float a,x,b,c,D,d;
int n;

public:
void Case1();
void Case2();
void Case3();
void Case4();
void Case5();
};

void Program::Case1()
{

cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..\n";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
D = pow(b,2) - (4*a*c);
setcolor(8);
if (D == 0)
{
cout << "The roots of this equation are real and equal!\n";
}
if (D > 0)
{
cout << "The roots of this equation are real and unequal!\n";
}
if (D < 0)
{
cout << "There are no roots to this equation..\n";
}
setcolor(7);
Sleep(3000);
}

void Program::Case2()
{

cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..\n";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
cout << "And the solutions are as follows -:\n";
setcolor(14);
D = pow(b,2) - (4*a*c);
float alpha = (-b - sqrt(D))/(2*a) ;
float beta =  (-b + sqrt(D))/(2*a) ;
setcolor(8);
if (D < 0)
{
cout << "This equation has no roots!\n";
}
else
{   cout << "x = " << alpha << endl;
cout << "x = " << beta << endl;
}
setcolor(7);
Sleep(3000);
}

void Program::Case3()
{
cout << "Enter coefficient 'a' - ";
cin  >> a;
cout << "\n";
cout << "Enter coefficient 'b' - ";
cin  >> b;
cout << "\n";
cout << "Enter constant 'c' - ";
cin  >> c;
cout << "\n";
cout << "The equation is..\n";
cout << a << "x^2 +" << b << "x +" << c << endl;
cout << "\n";
D = pow(b,2) - (4*a*c);
float alpha = (-b - sqrt(D))/(2*a) ;
float beta =  (-b + sqrt(D))/(2*a) ;
setcolor(8);
if (D < 0)
{
cout << "This equation has no roots!\n";
}
else
{ cout << "The sum of the roots is " << alpha + beta << endl;
cout << "The value of -b/a is " << -b/a << endl;
cout << "The product of the roots is " << alpha * beta << endl;
cout << "The value of c/a is " << c/a << endl;
}
setcolor(7);
Sleep(3000);
}

void Program::Case4()
{
cout << "Enter the first term - ";
cin >> a;
cout << "\nEnter the common difference - ";
cin >> d;
cout << "\nEnter the term number(must be an integer,not a decimal) - ";
cin >> n;
float t = a + (n-1) * d;
setcolor(14);
cout << "\nThe term t" << n << " is -> " << t << endl;
setcolor(7);
Sleep(3000);
}

void Program::Case5()
{
cout << "Enter the first term - ";
cin >> a;
cout << "\nEnter the common difference - ";
cin >> d;
cout << "\nEnter the term number(must be an integer,not a decimal) - ";
cin >> n;
float t = a + (n-1) * d;
float S = (n/2) * (a + t);
setcolor(13);
cout << "\nThe sum of the terms of this AP is " << S << endl;
setcolor(7);
Sleep(3000);
}

int main()
{
Program OMGDOIT;
int y;
do{

cout << "\nWelcome to the Equation Solver!\n";
cout << "\nChoose one of the following -:\n";
cout << "\n1. Find a quadratic equation's nature of roots\n";
cout << "\n2. Find a quadratic equation's roots\n";
cout << "\n3. Establish the Vi&#232;te relation of the roots and coefficients\n";
cout << "\n4. Find a term of an arithmetic progression(AP)\n";
cout << "\n5. Find the sum of the terms of an AP\n";
cout << "\n6. Quit,noobs >_>\n";
cin >> y;

switch(y)
{
case 1:
OMGDOIT.Case1();
system("PAUSE");
break;
case 2:
OMGDOIT.Case2();
system("PAUSE");
break;
case 3:
OMGDOIT.Case3();
system("PAUSE");
break;
case 4:
OMGDOIT.Case4();
system("PAUSE");
break;
case 5:
OMGDOIT.Case5();
system("PAUSE");
break;
case 6:
break;
default:
cout << "Enter a number in the list\n";
break;
}
}while (y != 6);
return 0;
}```

11. Originally Posted by CodeMonkey
Most square roots cannot be represented by a fraction. To estimate them using fractions would be misleading, since the presence of whole numbers would seem to indicate perfect precision.
I see...so we can let the whole number thingy be.