我目前正在用C ++编写一个基本的BMI计算器,使用类等。我有编码的基础知识,但是当我运行程序并输入我的参数时,它返回一个奇怪的值(-9.25596e + 061)
这是我的主要内容:
int main()
{
h_bmi bmi;
imperial calcImperial;
metric calcMetric;
char selection = '0';
cout << "Hello! Welcome to the BMI calculator! This calculator will ask you a few \nquestions for it to calculate your BMI!" << endl;
cout << "\n \nWould you like to use metric (Kilograms and meters) or would you like to use \nimperial (pounds and inches) measurements? (m/i)" << endl;
cin >> selection;
if (selection == 'm' || 'M')
{
cout << "Enter your weight in kilograms: " << endl;
cin >> calcMetric.weight;
cout << "Enter your height in meters: " << endl;
cin >> calcMetric.height;
cout << calcMetric.bmi << endl;
}
else if (selection == 'i' || 'I')
{
cout << "Enter your weight in pounds: " << endl;
cin >> calcImperial.weight;
cout << "Enter your height in inches: " << endl;
cin >> calcImperial.height;
cout << calcImperial.bmi << endl;
}
else
{
cout << "Please enter only 'm' for Metric, or 'i' for imperial!" << endl;
}
system("PAUSE");
return 0;
}
这是我的h_bmi.h
#pragma once
class h_bmi
{
public:
double height, weight, bmi;
h_bmi(){};
~h_bmi(){};
};
和我的imperial.h(我已将其包含在我的cpp文件中)
#include "h_bmi.h"
#include <cmath>
#pragma once
class imperial : public h_bmi
{
public:
double calcBMI(double height, double weight)
{
bmi = (weight * 703) / (pow(height, 2));
return bmi;
};
};
metric.h:
#include "h_bmi.h"
#include <cmath>
#pragma once
class metric : public h_bmi
{
public:
double calcBMI(double height, double weight)
{
bmi = weight / (pow(height, 2));
return bmi;
};
};
就像我说的那样,当我运行程序时,我输入&#34; m&#34;,然后&#34; 80&#34;然后&#34; 1.8&#34;但它会吹嘘这个价值。 如果有人能告诉我错误并指出我正确的方向,或者可能找到我的解决方案,我将不胜感激。
答案 0 :(得分:5)
您忘记了实际拨打calcBMI()