c - 我正在尝试使用 c 中的函数编写华氏到摄氏度的代码
问题描述
我正在尝试使用一个函数将华氏温度转换为摄氏温度。我想知道我在哪里犯了错误。
#include <stdio.h>
void degrees_Fto_C(float f){
float c;
c=(5/9)*(f–(32));
printf("%f",c);
}
int main(){
float temp;
printf("Enter the temperature in Fahrenheit: \n");
scanf("%f",&temp);
degrees_Fto_C(temp);
return 0;
}
错误信息是:
C:\Users\Pranavs\Desktop\Untitled1.cpp In function 'void degrees_Fto_C(float)':
C:\Users\Pranavs\Desktop\Untitled1.cpp [Error] 'f' cannot be used as a function
解决方案
第 4 行中 f 后面的字符有误。c=(5/9)*(f–(32))
需要是c=(5.0/9) * (f-(32))
. 你的减号是一个unicode字符,你需要ASCII。如果你退格它并用正常的减号替换它,它将编译。
此外,你正在做整数运算,你总是会得到零。如果在 5 或 9 之后添加小数点,效果会更好。
这是您的程序的工作版本:
#include <stdio.h>
void degrees_Fto_C(float f) {
float c;
c = (5.0 / 9) * (f - (32));
printf("%f", c);
}
int main() {
float temp;
printf("Enter the temperature in Fahrenheit: \n");
scanf("%f", &temp);
degrees_Fto_C(temp);
return 0;
}
推荐阅读
- c++ - 二维字符数组的膨胀/腐蚀
- autocomplete - HERE 自动完成 API:非常不准确的结果,忽略门牌号码
- python-3.x - 无法将字符串转换为十六进制 int 以在 chr() 中使用
- java - removeSpan(styleSpans[i]) 从样式文本中删除以前的样式
- php - 按组数据表计算行数
- docker - Docker compose 在烧瓶应用程序启动之前完全启动 mysql db
- django - 如何查找字符串是否在 Django 中的查询集的结果中
- mysql - SQL 有没有一种方法可以将多个时间列排序为一个,同时在没有 UNION 的情况下保留元数据?
- javascript - 页面加载时如何滑出元素?
- python - 在 Dask 中,可以在 Dask 中将张量重塑为 2D 矩阵而不预先计算大小吗?