新手求轻喷......
#include
using namespace std;
class People
{
public:
People(int i,float j);
~People(){}
float calculateBMI(int myWeight,float myHeight);
void getAnswer();
private:
int myWeight;
float myHeight,myBMI;
};
People::People(int i,float j)
{
myWeight=i;
myHeight=j;
}
float People::calculateBMI(int myWeight,float myHeight)
{
float myBMI;
myBMI=myWeight/(myHeight*myHeight);
return myBMI;
}
void People::getAnswer()
{
if(myBMI>=18.5&&myBMI<=23.9)
cout<<"No!"<<endl;
else
cout<<"Yes!"<<endl;
}
int main()
{
int myWeight;
float myHeight;
while(cin>>myWeight>>myHeight)
People x(int myWeight,float myHeight);
x.calculateBMI(myWeight,myHeight);
x.getAnswer();
return 0;
}
1.因为你的x是在while循环定义的,所以当循环条件不成立的时候相当于x没有定义
2.然后创建x的时候还有点问题,
People x(int myWeight,float myHeight);
参数只需要传变量名,改成
People x(myWeight,myHeight);
以下是我改的main函数,编译运行没问题
int main()
{
int myWeight=1;
float myHeight=1.5;
// while(cin>>myWeight>>myHeight)
People x(myWeight,myHeight);
x.calculateBMI(myWeight,myHeight);
x.getAnswer();
return 0;
}
希望帮到你
while(cin>>myWeight>>myHeight)
People x(int myWeight,float myHeight);
x.calculateBMI(myWeight,myHeight);
x.getAnswer();
-->
while(cin>>myWeight>>myHeight)
{
People x(myWeight,myHeight);
x.calculateBMI(myWeight,myHeight);
x.getAnswer();
}
#include
using namespace std;
class People
{
private:
int myWeight;
float myHeight;
float myBMI;
public:
People(int w,float h)
{
myWeight=w;
myHeight=h;
}
void calculateBMI()
{
myBMI=myWeight/(myHeight*myHeight);
}
void getAnswer()
{
calculateBMI();
if(myBMI>23.9)
{
cout<<"Yes!"< }
else
{
cout }
}
};
int main()
{
int w;
float h;
while(cin>>w>>h)
{
People people(w,h);
people.getAnswer();
}
return 0;
}
我觉得这样子写舒服一点