【问题标题】:keep throwing error : win chill index as 0 [closed]继续抛出错误:win chill index as 0 [关闭]
【发布时间】:2014-12-29 19:26:51
【问题描述】:

我继续运行代码,它总是将 win chill 指数显示为 0 我该如何解决这个问题?我的代码是

// include necessary libraries
#include <iostream>
#include <cmath>

using namespace std;
//prototype
double WC(double T, double v, double wc);

double WC(double T, double v, double wc)
// if statement for wind speed great than 4.8
{
    if (v > 4.8)
        wc = 13.12 + 0.6215* T - 11.37 * pow(v, 0.16) + 0.3965 * T * pow(v, 0.16);
    else wc = T;
        return wc;
}

// prototype 
void categories(double wc);

// function categories
void categories(double wc)
{
    //output
    cout << "Wind chill index is: " << wc << " degrees Celsius" << endl;
    //if-else statements for index
    if (wc <= 0 && wc > -25)
        cout << "This level of wind chill will cause discomfort." << endl;
    else if (wc <= -25 && wc > -45)
        cout << "This level of wind chill can have risk of skin freezing(frostbite)." << endl;
    else if (wc <= -45 && wc > -60)
        cout << "This level of wind chill will cause exposed skin to freeze within minutes" << endl;
    else if (wc <= -60)
        cout << "his level of wind chill will cause exposed skin to freeze in under 2 minutes" << endl;
    return;
}

//main function 
int main()
{
    double T;
    double v;
    double wc = 0;

    //prompt user for input
    cout << "Enter the current wind speed and temperature: " << endl;
    cin >> v >> T;
    //calling the functions
    WC(T, v, wc);
    categories(wc);

    return 0;

}

我认为这可能是因为我在我的主函数中将其声明为 wc=0,但在代码的前面我有一个设置 wc 值的方程,为什么不使用它?

【问题讨论】:

    标签: c++ function if-statement functional-programming


    【解决方案1】:

    第一次改变

    double WC(double T, double v, double wc);
    

    double WC(double& T, double& v, double& wc);
    

    WC(T, v, wc);
    

    wc = WC(T, v, wc);
    

    【讨论】:

    • 但是如果我添加 & 程序会正确返回 wc 但没有退出另一行“这种程度的风寒会引起不适”
    【解决方案2】:

    您的代码不起作用,因为在 C 中您应该像这样使用返回值

    wc=WC(T,v,wc)

    如果你看你的代码,你已经将返回值声明为双精度,所以使用它

    【讨论】:

      【解决方案3】:

      wc 是“按值传递”到函数 WC()。因此,在该函数中,wc 将是一个局部变量,并且不会反映到 main 中的 wc。由于您要返回 wc 的值,因此可以这样做:

      wc = WC();

      但是,在第一种情况下,如果 (v > 4.8),则必须返回 wc。

      另一种方法是“通过引用传递”。

      【讨论】:

        猜你喜欢
        • 2021-07-29
        • 1970-01-01
        • 2013-02-10
        • 2018-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-12
        相关资源
        最近更新 更多