我正在创建一个程序,其中基本上要求用户提供三个值,程序将接受这些值,然后将它们输入到二次公式中。我得到了二次公式,一切都有效。唯一的问题是,无论我作为用户输入放置是否重复该程序,它只是直接开始。我不能退出该计划。我一直在寻找解决方案,但没有雪茄。我应该提一下,我在编程方面没有那么多经验。但我会试着去理解我做错了什么。任何帮助都是很好的帮助
int main()
{
//variables
double a;
double b;
double c;
double x1;
double x2;
double discriminant;
char exeAgain='n';
do
{
cout<<"Please enter a value for A, B, and C to be used in the quadratic formula (and then press 'enter'): "<<endl;
cin>>a>>b>>c; //gets the values for A, B, and C
//the equations for the quadratic formula
discriminant=(b*b-(4*a*c));
x1= (-1*b + (sqrt(discriminant)))/(2*a);
x2= (-1*b - (sqrt(discriminant)))/(2*a);
if(discriminant>0) //if inside the sqrt is more than 0 (desired)
{
cout<<"The roots of " <<a<< "x^2 + " <<b<< "x + " <<c<< " is/are: " <<endl;
cout<<x1<<" and "<<x2<<endl;
}
else if(discriminant==0) //if inside the sqrt is 0
{
cout<<"The roots of " <<a<< "x^2 + " <<b<< "x + " <<c<< " is " <<endl;
cout<<x1<<endl;
}
else //the discriminant is less than 0 (leads to complex roots)
{
discriminant = -1 * discriminant; //used to change the discriminant to a positive #
cout<<"The roots of " <<a<< "x^2 + " <<b<< "x + " <<c<< " is/are: " <<endl;
cout<<"("<<(-1*b)<<" + "<<sqrt(discriminant)<<"i)/"<<2*a<<" and ("<<(-1*b)<<" - "<<sqrt(discriminant)<<"i)/"<<2*a<<endl;
}
cout<<"Would you like to execute this program again? Type in Y or y for yes, or any other letter to quit the program: ";
cin>>exeAgain;
} while(exeAgain == 'y' || exeAgain == 'Y');
return 0;
}
答案 0 :(得分:0)
你正在进行OR ||
检查。你的第二个条件总是评估为真。
将其更改为:
while(exeAgain == 'y' || exeAgain == 'Y');
答案 1 :(得分:0)
使用exeAgain != 'y' || exeAgain != 'Y'
。
当前表达式exeAgain != 'y' || 'Y'
表示“exeAgain
与'y'
不同,或'Y'
不为零。由于'Y'
是一个不等于零的文字常量,循环总是重复。