Quadratic Equation Program Assistance
Oct 29, 2014 at 4:16pm UTC
For my next C++ class assignment, I need to write a program which can give the value of "x" in a quadratic equation when inputting the values of a, b, and c. I've written up a program which largely accounts for all the scenarios, and which checks out in the compiler. However, when I go to run it, and I put in values for a, b, and c, the program seems to skip back to the input section. Does anyone know what I did wrong in this?
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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
ofstream output ("c:result.dat" );
int a,b,c;
double d,x1,x2;
while (a+b+c>0)
{
cout<<"Enter the value of a, b and c. " ;
output<<"Enter the value of a, b and c. " ;
cin>>a>>b>>c;
d=pow(b,2)-4*a*c;
if ((a+b+c)<=0)
{
break ;
}
if (a=0)
{
if (b=0)
{
cout<<"This is not an equation." <<endl;
output<<"This is not an equation" <<endl;
}
else
{
cout<<"The equation is linear. x = " <<(-c/b)<<endl;
output<<"The equation is linear. x = " <<(-c/b)<<endl;
}
}
if (d>0)
{
x1=(-b/(2*a))+(sqrt(d)/(2*a));
x2=(-b/(2*a))-(sqrt(d)/(2*a));
cout<<"x1 = " <<x1<<endl;
cout<<"x2 = " <<x2<<endl;
output<<"x1 = " <<x1<<endl;
output<<"x2 = " <<x2<<endl;
}
else if (d=0)
{
cout<<"x = " <<(-b/(2*a))<<endl;
output<<"x = " <<(-b/(2*a))<<endl;
}
else if (d<0)
{
cout<<"The answers are complex numbers." <<endl;
cout<<"x1 = " <<(-b/(2*a))<<" + " <<(sqrt(-d)/(2*a))<<"i" <<endl;
cout<<"x2 = " <<(-b/(2*a))<<" - " <<(sqrt(-d)/(2*a))<<"i" <<endl;
output<<"The answers are complex numbers." <<endl;
output<<"x1 = " <<(-b/(2*a))<<" + " <<(sqrt(-d)/(2*a))<<"i" <<endl;
output<<"x2 = " <<(-b/(2*a))<<" - " <<(sqrt(-d)/(2*a))<<"i" <<endl;
}
}
return 0;
}
Oct 29, 2014 at 5:09pm UTC
Hint: A single "=" is the assignment operator.
Oct 29, 2014 at 5:17pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a = 10 , b= -20 , c = -30;
//ax^2 + bx +c = 0
double x1 = (-1*b + sqrt(b*b - 4*a*c) ) /(2*a) ;
double x2 = (-1*b - sqrt(b*b - 4*a*c) ) /(2*a) ;
cout << x1 << " " << x2; // 3 -1
return 0;
}
Topic archived. No new replies allowed.