C++算术出错·怎么出现错误的提示·

来源:百度知道 编辑:UC知道 时间:2024/05/15 12:40:38
#include "iostream.h"
int main()
{
float r;
float l;
const float pi=3.1415926f;
cout <<"请输入半径: ";
cin >>r;
l=2*pi*r;
cout <<"这个圆的周长是: " <<l <<endl;
return 0;
}
这个是正确的·
书上要我修改次程序,使得输入半径为非正数时能输出错误提示·

#include "iostream.h"
int main()
{
float r;
float l;
const float pi=3.1415926f;
cout <<"请输入半径: ";
cin >>r;
l=2*pi*r;
if(l>0)cout <<"这个圆的周长是: " <<l <<endl;
else cout<<"输入错误:半径小于零!"<<endl;
return 0;
}