C++ 持续抛出错误:win chill索引为0
我一直在运行代码,它总是将win chill索引显示为0。如何修复此问题?我的代码是C++ 持续抛出错误:win chill索引为0,c++,function,if-statement,functional-programming,C++,Function,If Statement,Functional Programming,我一直在运行代码,它总是将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 sta
// 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值的等式,为什么不使用它?第一个更改
double WC(double T, double v, double wc);
到
或
到
您的代码不起作用,因为在C中您应该像这样使用返回值 wc=WCT,v,wc 如果查看代码,您已经将返回值声明为double,因此使用它wc通过值传递给函数wc。因此,在该函数中,wc将是一个局部变量,并且该变量不会反映到主函数中的wc。由于您正在返回wc的值,因此可以这样做: wc=wc 但是,在第一种情况下,如果v>4.8,则必须返回wc
另一种方法是通过引用进行传递。但是如果我添加了&程序正确返回wc,但没有返回到另一行,那么这种程度的风寒将导致不适
double WC(double& T, double& v, double& wc);
WC(T, v, wc);
wc = WC(T, v, wc);