简单的功能C++
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 方法之外声明函数。 在
- main 方法之前声明函数或使用前向声明
- 将"值"作为参数传递给每个函数。
- 删除不必要的变量声明。
- 对用户输入进行一些验证。
- 使用有意义的变量名称。
包括
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;
}
首先,你不能在main()函数中声明另一个函数。
其次,所有函数都有一个返回类型,但令人惊讶的是,你调用它们,因为它们是 void。使函数无效,而不是返回类型。比如....
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;
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?