Quadratic Equation
Oct 7, 2013 at 2:53am UTC
This is my code for the quadratic equation. It keeps telling me that my else is illegal since no matching if statement and my else statement is missing a statement
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
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string Name;
double a;
double b;
double c;
double discriminant;
double x1;
double x2;
cout << scientific;
bool bFail;
cout << "\t\t\t===============================\n" ;
cout << "\t\t\t\tQuadratic Equation\n" ;
cout << "\t\t\t===============================\n" ;
cout << "Full Name:" ;
getline(cin,Name);
cout << "\n\n" ;
cout << setw(25) << Name << "\n" ;
cout << "This program will provide solutions for an equation of the form:\n" ;
cout << "\t\ta*x^2 + b*x + c = 0\n" ;
cout << "Where a,b, and c are integers and a is not equal to zero\n" ;
do {
cout << "Enter value of a:" ;
cin >> a;
if ( a = 0);
{ cout << "No solution!\n" ;
}
else if ( a != 0)
{
bFail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n' );
}
}while (bFail == true );
do {
cout << "Enter value of b:" ;
cin >> b;
bFail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n' );
}while (bFail == true );
do {
cout << "Enter value of c:" ;
cin >> c;
bFail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n' );
}while (bFail == true );
discriminant = pow(b,2) - 4 * a * c;
return 0;
}
Oct 7, 2013 at 2:58am UTC
if ( a = 0); // <--- Remove the semicolon - use == operator
Last edited on Oct 7, 2013 at 2:59am UTC
Oct 7, 2013 at 3:02am UTC
thank you so much! but now the problem is that it says that bFail is being ran without being initialized and the programming keeps running. how can i avoid this?
Oct 7, 2013 at 3:40am UTC
Exactly as it says ;)
You declared your Boolean bFail and have it tested in your do-while loop, but you never set it to true or false before hand.
Topic archived. No new replies allowed.