2015-06-25 67 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或輸出)的「默認」模式中,精度是的位數打印,小數點的兩側。認爲「有意義的數字」,而不是「小數位數」。

對於你正在嘗試做的事情,我建議你要麼使用「固定」模式,要麼手工循環,然後不指定精度。

相關問題