我一直在运行代码,它总是将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值为什么不使用?
答案 0 :(得分:0)
首先改变
double WC(double T, double v, double wc);
到
double WC(double& T, double& v, double& wc);
OR
WC(T, v, wc);
到
wc = WC(T, v, wc);
答案 1 :(得分:0)
您的代码不起作用,因为在C中您应该像这样使用返回值
wc=WC(T,v,wc)
如果查看代码,则已将返回值声明为double,因此请使用
答案 2 :(得分:0)
wc是“按值传递”到函数WC()。因此,在该函数中,wc将是一个局部变量,并且不会反映到main中的wc。既然你要返回wc的值,你可以这样做:
wc = WC();
然而,在你的第一种情况下,如果(v> 4.8),你将不得不返回wc。
另一种方法是“通过引用传递”。