gpt4 book ai didi

c++ - 控制台应用程序返回一个奇怪的值

转载 作者:太空宇宙 更新时间:2023-11-04 16:12:52 25 4
gpt4 key购买 nike

我目前正在用 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;
};

};

指标.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;
};

};

就像我说的,当我运行程序时,我输入“m”,然后输入“80”,然后输入“1.8”,但它会计算出这个值。如果有人能告诉我错误并指出正确的方向,或者可能找到修复方法,将不胜感激。

最佳答案

您忘记实际调用 calcBMI()

关于c++ - 控制台应用程序返回一个奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26362103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com