【问题标题】:BMI Calculator If statement being skipped [closed]BMI计算器如果语句被跳过[关闭]
【发布时间】:2014-11-13 15:40:40
【问题描述】:

在我的

if 语句

程序继续跳到"you are over weight" 并显示在顶部而不是bmi 旁边。

输出:http://i.imgur.com/1SF2cPc.png

另外,我的平均值似乎计算不正确。

如何正确计算平均 BMI?

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    //declaring variables
    string name;
    int inches;
    double bmi, weight, noNames=0.0;
    double average_bmi;
    const double kilogram=0.45359237;
    const double metres = 0.0254;

    ofstream fout("bmi.dat");
    if (!fout.is_open())
    {
        cout << "error opening file ";
        system("pause");
        exit(-1);
    }
    fout << fixed << setprecision(2);

    fout << "Body Mass Index Report" << endl << endl;
    fout << left << setw(20) << "Name" << setw(20) << "Weight" << right << setw(10) << "Height" << setw(10) << "BMI"
        << setw(15) << endl << endl;

    cout << "Enter Your Name (crtl+Z to exit) ";
    getline(cin, name);
    while (!cin.eof())
    {
        cout << "Enter height in inches:";
        cin >> inches;
        while (cin.fail() || inches <=0)  //validate height for a number +'ve & >0
    {
        cin.clear();
        cin.ignore(80, '\n'); 
        cout << "Please enter a valid number ";
        cin >> inches;
    }
        cout << "Enter your weight (lbs): ";
        cin >> weight;
        cout << endl;

        while (cin.fail() || weight <= 0)
        {
            cin.clear();
            cin.ignore(80, '\n');
            cout << "Please enter a valid number  ";
            cin >> inches;
    }

        bmi = weight * kilogram / (inches * metres * inches * metres);

            cout << "Your Body Mass Index is  " << bmi;
        {   
            if (bmi > 18.5)
                fout << "  You are underweight.";
            else if ((bmi >= 18.5) && (bmi < 24.9))
                fout << "  You are normal weight.";
            else if (bmi < 25)
                fout << "  you are overweight." << endl << endl;
        }

        noNames++; //count number of names

        fout << left << setw(20) << name << setw(20) << weight << right << setw(10) << inches << setw(10)
            << bmi << setw(15) << endl;

        average_bmi = bmi++ / noNames++;
        fout << endl << endl << "Average bmi: " << average_bmi << endl;

        cout << ". Enter another name or ^Z to exit ";
        cin.ignore(80, '\n');
        getline(cin, name);
    }                

    if (noNames<0)
    {
        fout << "No values given " << endl;
    }
    cout << "program ended successfully" << endl;

    system("type bmi.dat");
    system("pause");
}

【问题讨论】:

  • 你不是说bmi &gt; 25(也就是大于25)
  • ... 和 bmi
  • 是的,但是当我将其更改为正确的方式时,它根本不会显示所需的评论。 (即:您正常/体重不足/超重)。当值被错误分配时,它只会说“你超重”。
  • @Robert 我被告知“通过将总 BMI 除以姓名的数量来计算平均 BMI。”

标签: c++ calculator


【解决方案1】:

您在比较 BMI 时切换了“”:

        if (bmi < 18.5)
            fout << "  You are underweight.";
        else if ((bmi >= 18.5) && (bmi < 24.9))
            fout << "  You are normal weight.";
        else if (bmi > 25)
            fout << "  you are overweight." << endl << endl;

顺便说一句,最后一个if 是多余的。 else 就够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多