2015-06-25 61 views
1

请注意下面的家庭作业。

编辑:

拿出无用的信息。为什么这些函数会截断返回值?

所以很明显,这是一项家庭作业的任务除了我的函数内部的计算外,一切似乎都是正确的。

如何返回非截断值?

float hat(float weight, float height) { 
    return (weight/height)*2.9; 
} 
float jacket(float weight, float height, int age) { 
    double result = (height * weight)/288; 
    /*now for every 10 years past 30 add (1/8) to the result*/ 
    if((age - 30) > 0){ 
     int temp = (age - 30)/10; 
     result = result + (temp * .125); 
     //cout<<"result is: "<<result<<endl; 
    } 
    return result; 
} 

float waist(float weight, int age) { 
    double result = weight/5.7; 
    /*now for every 2 years past 28 we add (1/10) to the result*/ 
    if((age - 28) > 0){ 
     int temp = (age - 28)/2; 
     result = result + (temp * .1); 
    } 
return result;} 
+1

您正在输入的顺序错误。学习使用调试器 – Amit

+0

我只是想出了。现在,这些值将被截断或最大值。我要更新这个问题。 – Rekumaru

+0

有*理由*我们要求[最小完整示例](http://stackoverflow.com/help/mcve)。 – Beta

回答

0

fixed

// Output data // 
    cout << fixed; 
    cout << "hat size: " << setprecision(2) << hat(weight, height) << endl; 
    cout << "jacket size: " << setprecision(2) << jacket(weight, height, age) << endl; 
    cout << "waist size: " << setprecision(2) << waist(weight, age) << endl; 
1
cout << "hat size: " << setprecision(2) << hat(weight, height) << endl; 

你绊倒在IOSTREAMS格式化输出工作方式的疑难杂症。

在用于格式化浮点值(不具有请求fixedscientific或输出)的“默认”模式中,精度是的位数打印,小数点的两侧。认为“有意义的数字”,而不是“小数位数”。

对于你正在尝试做的事情,我建议你要么使用“固定”模式,要么手工循环,然后不指定精度。

相关问题