【发布时间】: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