C++编程题

来源:百度知道 编辑:UC知道 时间:2024/06/01 14:33:45
编程序求半径为r、高为h的圆柱体的体积
#include "iostream.h"
void main()
#define PI 3.14
{
float r;
float h;
float V=PI*r*r*h;
cout<<"V="<<V<<endl;
}
我的程序如上,但是结果出来不含h、r,为什么?
应该怎么编,谢谢~

你没有对r,h初始化,要不就写cin>>r>>h;要不就直接赋值,r=2,h=1.你的程序有点小小不足,如果V是float,那么会有句这样的警告:
warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
所以V最好是double型,这样就没有警告出现了
#include "iostream.h"
void main()
#define PI 3.14
{
float r=1.0;
float h=1.0;
cin>>r>>h;
double V=PI*r*r*h;
cout<<"V="<<V<<endl;
}

#include "iostream.h"
#define PI 3.14
void main()
{
float r;
float h;
cin>>r>>h;
V=PI*r*r*h;
cout<<"V="<<V<<endl;
}
首先你的书写的格式并不好
你在计算V时应该给r,h赋值啊,要不他们值是多少啊???
这里cin就是vc提供的输入的类

你没有对半径和高赋值,程序怎么计算啊
#include "iostream.h"
void main()
#define PI 3.14
{
float r=3,h=2.5;
float V=PI*r*r*h;
cout<<"V="<<V<<endl;
}