[英]Simple Function C++
using namespace std;
int main()
{
return 0;
}
double C2F()
{
f = value * 9 / 5 + 32;
return f;
}
double K2F()
{
f = (value - 273.15) * 1.8 + 32.0;
return f;
}
double N2F()
{
f = value * 60 / 11 + 32;
return f;
}
我在调用这些函数来计算温度转换而不是从案例中计算温度转换时遇到麻烦。 添加这些功能后,该程序甚至无法编译。 “错误:预期为“;””
您不能在另一个函数中声明或定义函数。 将您的定义移到int main(){ ... }
。
首先,您不能在main()函数中声明另一个函数。
其次,所有函数都具有返回类型,但是令人惊讶的是您正在调用它们,因为它们是无效的。 使该函数无效,而不是返回类型。 例如...
void C2F()
{
f = value * 9 / 5 + 32;
}
接着
case 'C':
C2F();
cout << value << "C is " << f << " in Farenheit" << endl;
break;
要么。 您可以在双精度类型变量中接收返回值并打印该值。
case 'C':
cout << value << "C is " << C2F() << " in Farenheit" << endl;
break;
这就是你想要的。
using namespace std;
double C2F(double f)
{
return f * 9 / 5 + 32;
}
double K2F(double f)
{
return ((f - 273.15) * 1.8 + 32.0);
}
double N2F(double f)
{
return (f * 60 / 11 + 32);
}
int main()
{
char function;
double value;
cout << "This temperature Conversion program converts other temperatures to farenheit" << endl;
cout << "The temperature types are" << endl;
cout << "" << endl;
cout << "C - Celcius" << endl;
cout << "K - Kelvin" << endl;
cout << "N - Newton" << endl;
cout << "X - eXit" << endl;
cout << "" << endl;
cout << "To use the converter you must input a value and one of the temperature types." << endl;
cout << "For example 32 C converts 32 degrees from Celsius to Fahrenheit" << endl;
cin >> value >> function;
function = toupper(function);
while (function != 'X')
{
switch (function)
{
case 'C':
cout << value << "C is " << C2F(value) << " in Farenheit" << endl;
break;
case 'K':
cout << value << "K is " << K2F(value) << " in Farenheit" << endl;
break;
case 'N':
cout << value << "N is " << N2F(value) << " in Farenheit" << endl;
break;
default:
cout << "Correct choices are C, K, N, X" << endl;
}
cout << "Please enter a value and it's type to be converted" << endl;
cin >> value >> function;
function = toupper(function);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.