我在这个公式中缺少什么?

时间:2017-05-07 21:29:56

标签: c++

我无法弄清楚为什么这个公式会导致0.代码会编译,但当我要求它返回'bloodAlc'的结果时,它总是说0.我检查了int和double,但是至于我知道,所有的返回数据都应该是双倍的。

    #include<iostream>
    #include<fstream>
    #include<string>
    #include "Input_Validation_Extended.h"

int main () {
using namespace std;

ifstream qFile ("Final.txt");
string qLine;

while (!qFile.eof() ) {
    getline (qFile, qLine);
    cout << qLine << endl;

} 
qFile.close();

char gender;
double genderConst, weight, percentAlc, hours, ounces, bloodAlc, alcType;
double beer, wine, spirits; 
weight = 0;
hours = 0;
ounces = 0;
bloodAlc = 0.0;
cout << "Please enter your weight in pounds: \n";
cin >> weight;

cout << "Please enter your gender: \n";
cin >> gender;

cout << "Please enter the amount of hours you have been drinking: \n";
cin >> hours;

cout << "How many drinks have you had: ";
cin >> ounces;

cout << "Please choose A, B, or C for the type of beverage you are enjoying: ";
cin >> alcType;

if (gender == 'F' || gender == 'f')
{
    genderConst = .66;
}
else if (gender == 'M' || gender == 'm')
{
    genderConst = .73;
}   
if (alcType == 'A' || alcType == 'a')
{
beer = (12.0 * ounces);
bloodAlc = ((beer * 5.14) / (weight * genderConst)) - (.015 * hours);
}
else if (alcType == 'B' || alcType == 'b')
{
wine = (5.0 * ounces);
bloodAlc = ((wine * 5.14) / (weight * genderConst)) - (.015 * hours);
}

 cout << "Your BAC is: " << bloodAlc;

return 0;
}

1 个答案:

答案 0 :(得分:2)

您将alcType声明为double,但您将其用作char

double genderConst, weight, percentAlc, hours, ounces, bloodAlc, alcType;
...
if (alcType == 'A' || alcType == 'a')

因此,不会输入任何if (alcType == 'A'..) - 路径,bloodAlc仍为0.0

double alcType更改为char alcType,它应该有效。 如果没有,请使用调试器: - )