Hey guys, a bit of a C++ beginner here, just need some help figuring out where to add a while loop that will ask the user if they want to solve another equation with a (y/n). Code below works and looks like it supposed to, just need a loop in here. Any help would be awesome.
Thanks for the reply if i want it to recognize a capital "Y" and also the lower case "y" , how would i set that variable up? I know it has to do with the ll command. Would it be something like this?
When dealing with the || (Logical OR) operator (and every other operator for that matter), each statement is separate from the other. You must say it twice, as in: while (another_calc == 'y' || another_calc == 'Y')
Edit: Also that isn't how you initialize a char, (x || y) returns a bool, not a char. Just declare it and you can initialize it using cin as you seem to already be doing.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double a, b, c;
double root1;
double root2;
double discriminant;
char another_calc;
cout.setf(ios::fixed); //
cout.setf(ios::showpoint); //Magic Formula
cout.precision(2); //
do
{
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
cout << "For the equation " << a << "(x * x) + " << b << "x + " << c << " = 0\n";
discriminant = (pow(b,2) - 4*a*c);
root1 = (((-b) + sqrt(discriminant))/(2*a));
root2 = (((-b) - sqrt(discriminant))/(2*a));
if (discriminant > 0)
{
cout << "The roots are " << root1 << " and " << root2 <<endl;
}
else
{
cout << "No real roots.\n";
}
cout << "Do you want to solve another equation (y/n)?";
cin >> another_calc;
}
while (another_calc == 'y' || another_calc == 'Y');
if (another_calc != 'y' || another_calc != 'Y')
{
cout << "End of program\n";
}
return(0);
}
For some reason i just cant get a normal while loop to work. If someone could edit the code for a normal while loop that would be cool, id like to see both.