NaN问题啊

来源:百度知道 编辑:UC知道 时间:2024/05/27 05:24:30
求方程的根,当输入a=1,b=2,c=1时怎么出现不了正确答案呢
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double a,b,c,x1,x2,disc;
do{
cout << "input three numbers:";
cin >> a,b,c;
disc=b*b-4*a*c;
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
cout << "x1=" << x1 << endl;
cout << "x2=" << x2 << endl;
if(disc<0)
cout << "input again:";
}while(disc>=0);
}

出现不了正确答案是因为你的输入语句有问题,应该写成cin >> a >> b >> c;
但是你的while确实写的也不对

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double a,b,c,x1,x2,disc;
do{
cout << "input three numbers:";
cin >> a>>b>>c;
disc=b*b-4*a*c;
}while(disc<0);
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
cout << "x1=" << x1 << endl;
cout << "x2=" << x2 << endl;
}

用IF语句和FOR语句的组合试下,WHILE语句掌握不好就最好别用