Search code examples
c++functionarithmetic-expressionstruncated

Why do these functions truncate the return value?


Beware there be Homework below.

Edits:

Took out useless information.

So obviously this is a homework assignment everything seems correct besides the calculations inside my functions.

How do I return a non truncated value?

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;}

Solution

  • Set 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;