无法让我的if / else语句在用户定义的函数中工作

时间:2015-10-09 14:54:47

标签: c++ conditional statements

我目前正在编写一个程序,输出用户的帽子大小,夹克大小和腰围尺寸。

帽子尺寸=以磅为单位的重量除以以英寸为单位的高度以及所有乘以2.9

的重量

夹克尺寸(胸围以英寸为单位)=身高乘以重量除以288然后通过在30岁以上每10年增加1/8英寸进行调整。(请注意,调整仅在完整后进行10年,30-39之间没有调整,但40岁时增加了1/8英寸。)

以英寸为单位的腰围=重量除以5.7然后通过在28岁以上每2年增加1/10英寸进行调整,整整两年,因此没有调整29,而是1/10英寸将在30年后添加。

这是我的代码,运行并输出正确的值,假设用户没有得到任何调整,例如年龄:18。

我在下面的声明中设置了if / else语句,但是在返回时它们似乎没有工作。

要求大多数(如果不是全部)计算都需要在函数中执行。

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>


using namespace std;


double hat_size(double weight, double height);
double jacket_size(double height, double weight, double age);
double waist_size(double weight, double age);

int main()
{
    double height, weight, age;
    double Hat_size, Jacket_size, Waist_size;
    char answer;

    do
    {

        cout << "Please enter the users age: ";
        std::cin >> age;


        cout << "\n\nPlease enter users height in inches: ";
        std::cin >> height;

        cout << "\nPlease enter the users weight in pounds: ";
        std::cin >> weight;




        Hat_size = hat_size(weight, height);
        Jacket_size = jacket_size(height, weight, age);
        Waist_size = waist_size(weight, age);

        cout << "\nHat Size is: "
             << Hat_size;
        cout << "\nJacket Size is: "
            << Jacket_size
            << " inches";
        cout << "\nWaist Size is: " 
             << Waist_size
             << " inches";


        /*  

        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);

        */

        cout << "\n\nDo you want to preform another calculation? Please type Y:\n";
        std::cin >> answer;
    } while (answer == 'y' || answer == 'Y' || answer == 'yes' || answer == 'Yes');


    return 0;
}

double hat_size(double weight, double height)
{
    return((weight / height)*2.9);
}

double jacket_size(double height, double weight, double age)
{
    double j_size, j_age_shrink, jacket_adjustment, j_change;

    j_size = ((height * weight) / 288);
    jacket_adjustment = 1 / 8;
    j_age_shrink = (age / 10 - 3) * jacket_adjustment;
    j_change = j_size + j_age_shrink;

    if ((age / 10) >= 4)
    {
        return (j_change);
    }
    else
    {
        return (j_size);
    }
}


double waist_size(double weight, double age)
    {
        double w_size, w_age_shrink, waist_adjustment, w_change;

        w_size = (weight / 5.7);

        waist_adjustment = 1 / 10;
        w_age_shrink = ((age - 28) / 2) * waist_adjustment;
        w_change = w_size + w_age_shrink;

        if (age >= 30)
        {
            return(w_change);
        }
        else
        {
            return (w_size);
        }
    }

0 个答案:

没有答案