I wrote this code for the quadratic equation but I want to know how can I use cin.fail() when someone enters an invalid character for a b or c. I don't want the program to end, I want the user to enter a correct numerical value for a b or c.
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string Name;
double a;
double b;
double c;
double discriminant;
double x1;
double x2;
double y;
double z;
cout << scientific;
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";
cout << "Enter value of a:";
cin >> a;
if(a == 0)
{ cout << "No solutions will be calculated for a leading coefficient of 0\n";
exit(0);
}
elseif(a != 0)
{ cout << "Enter value of b:";
cin >> b;
cout << "Enter value of c:";
cin >> c;
}
discriminant = pow(b,2) - 4 * a * c;
if(discriminant > 0)
{ abs(discriminant);
x1 = (((-b) + sqrt(discriminant))/(2*a));
x2 = (((-b) - sqrt(discriminant))/(2*a));
cout << "The two real solutions are \n x =" << x1 << "\n" << "and x =" << x2 << "\n";
}
elseif(discriminant == 0)
{ z = (-b)/(2*a);
cout << "The only real solution is " << z << "\n";
}
elseif(discriminant < 0)
{ y = discriminant * -1;
x1 = (((-b) + sqrt(y))/(2*a));
x2 = (((-b) - sqrt(y))/(2*a));
cout << "The two imagninary solutions are x = " << x1 << "*i" << "\n" << "and x = " << x2 << "*i\n";
}
return 0;
}
I know I have to put this code somewhere in the overall code
1 2 3 4 5 6 7
do{
//the cin for the values a b or c are suppose to be in here somehwo
bFail = cin.fail()
cin.clear
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}while(bFail == true);
- Line 44: b * b is more appropriate than pow(b,2) !
- Line 47: why abs(discriminant) while discriminant > 0 ?
- Line 62: when discriminant < 0 the quadratic equation has two complex conjugates roots: - b / (2.0 * a) +/- i * sqrt( - discriminant) / (2.0 * a) so the line must be corrected.
PS The problem with the common fail/clear approach is that it admits strings which begin with a number but are not just a number. Using code below to drive MiiNiPaa's function (tweaked to prohibit negative numbers) I get, for this sequence of inputs:
12
42nd Street
3 4 5
3.14
-273
0
this output (I've enboldened the text I types in):
Enter one or more positive int values
Enter zero (0) to exit
Enter value: 12
got: 12
Enter value: 42nd Street
got: 42
Enter value: invalid value : please enter a positive integer value, or 0 to exit
3 4 5
got: 3
Enter value: got: 4
Enter value: got: 5
Enter value: 3.14
got: 3
Enter value: invalid value : please enter a positive integer value, or 0 to exit
-273
invalid value : please enter a positive integer value, or 0 to exit
0
got: 0
Whereas if I feed the same set of inputs to cire's function I get:
Enter one or more positive int values
Enter zero (0) to exit
Enter value: 12
got: 12
Enter value: 42nd Street
invalid value : please enter a positive integer value, or 0 to exit
Enter value: 3 4 5
invalid value : please enter a positive integer value, or 0 to exit
Enter value: 3.14
invalid value : please enter a positive integer value, or 0 to exit
Enter value: -273
invalid value : please enter a positive integer value, or 0 to exit
Enter value: 0
got: 0
Code listings:
#1 -- using tweaked version of MiiNiPaa's function (which now checks value is positive or zero.)