c++问题'sqrt' : undeclared identifier

#include<iostream>
using namespace std;

int main()
{
float a, b, c, x1, x2, dlt;

cout<<"input 3 parameters:";
cout<<"input a: ";
cin>>a;
cout<<"input b: ";
cin>>b;
cout<<"input c: ";
cin>>c;

dlt = b * b - 4 * a * c;
if(dlt >= 0)
{
x1 = (-b + sqrt(dlt)) / 2 / a;
x2 = (-b - sqrt(dlt)) / 2 / a;
cout<<x1<<x2;
}
else
{
cout<<"no root";
}
return 0;
}

  需要添加头文件引用,在代码顶头添加如下代码:
  #include <math.h>
  这是因为sqrt是属于数学函数库里面的函数,如果要使用它,需要添加数学库支持头文件
  另外,在C/C++语言中,要使用一个函数,需要先声明再使用。而这些头文件里面都是对函数的声明代码。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-11
加上预处理命令#include <math.h>
函数sqrt()是math.h也就是数学函数库里的函数之一,因此调用它时一般要用#include把这个头文件包含到程序中本回答被提问者采纳
第2个回答  2009-03-07
加上 #include <math.h> 或者 #incluce <cmath>
相似回答