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

标签 c++

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

相关文章:

c++ - 超过 4 个字节的整数是什么类型?

c++ - C++11初始化变量时{}和=的区别

c++ - 帮助 map C++

c++ - 如何将模数运算符与其他数据类型一起使用?

c++ - 如何将字节打印为十六进制?

c++ - 如何使用 std::reverse_iterator 删除 *AND CONTINUE*?

c++ - STL 容器的基于范围的循环中的迭代器语法有什么区别

c++ - 将 pickle 添加到用 C++ 编写的 Python 扩展

c++ - 通过 SendMessage 进行 IPC 时如何处理 32 位/64 位不匹配?

c++ - 没有指定模板参数但仍然有效