C简单温度转换器/摄氏度到华氏度未知错误&;输出
我几天前刚开始用C编程,有几个问题: 以下程序将摄氏度转换为华氏度,反之亦然。我得到一个分段错误C简单温度转换器/摄氏度到华氏度未知错误&;输出,c,converter,temperature,C,Converter,Temperature,我几天前刚开始用C编程,有几个问题: 以下程序将摄氏度转换为华氏度,反之亦然。我得到一个分段错误 #include<stdio.h> #include <string.h> #include<stdlib.h> float c2f(float); float f2c(float); float Fahrenheit,Celsius; int main(int argc, char *argv[]) { /** * Check for the exp
#include<stdio.h>
#include <string.h>
#include<stdlib.h>
float c2f(float);
float f2c(float);
float Fahrenheit,Celsius;
int main(int argc, char *argv[])
{
/**
* Check for the expected number of arguments (3)
* (0) program name
* (1) flag
* (2) temperature
*/
if (argc!=3)
printf("Incorrect number of arguments");
if (!strcmp(argv[1], "->f"))
{
// convert the string into a floating number
char *check;
float Celsius = strtod(argv[2], &check);
// process from celsius to fahrenheit
Fahrenheit = c2f(Celsius);
printf("%5.2f°C = %5.2f°F",Celsius, Fahrenheit);
}
else if (!strcmp(argv[1], "->c"))
{
// convert the string into a floating number
char *check;
float Fahrenheit = strtod(argv[2], &check);
// process from fahrenheit to celsius
Celsius = f2c(Fahrenheit);
printf("%5.2f°F = %5.2f°C", Fahrenheit, Celsius);
}
else
printf("Invalid flag\n");
} // main
float c2f(float c)
{
return 32 + (c * (180.0 / 100.0));
}
float f2c(float f)
{
return (100.0 / 180.0) * (f - 32);
}
#包括
#包括
#包括
浮动c2f(浮动);
浮动f2c(浮动);
浮动华氏温度,摄氏度;
int main(int argc,char*argv[])
{
/**
*检查预期的参数数(3)
*(0)程序名
*(1)旗帜
*(2)温度
*/
如果(argc!=3)
printf(“参数数量不正确”);
如果(!strcmp(argv[1],“->f”))
{
//将字符串转换为浮点数
字符*检查;
浮动摄氏度=标准偏差(argv[2],&检查);
//从摄氏度到华氏度的过程
华氏温度=c2f(摄氏度);
printf(“%5.2f°C=%5.2f°F”,摄氏度,华氏度);
}
如果(!strcmp(argv[1],“->c”))
{
//将字符串转换为浮点数
字符*检查;
浮动华氏温度=标准温度(argv[2],&检查);
//从华氏温度到摄氏温度的过程
摄氏度=f2c(华氏度);
printf(“%5.2f°F=%5.2f°C”,华氏度,摄氏度);
}
其他的
printf(“无效标志\n”);
}//主要
浮球c2f(浮球c)
{
返回32+(c*(180.0/100.0));
}
浮点数f2c(浮点数f)
{
回报率(100.0/180.0)*(f-32);
}
此外,我希望我的输出如下:
**>温度转换器->f 10.0
10.00°C=50.00°F**
这应将10C转换为F
对于F到C,输出应为:
温度转换器->c 50.0
50.00°F=10C**误差为
如果(!strcmp(argv[1],“->f”)
缺少最后一个括号,应该是
if (!strcmp(argv[1], "->f"))
你犯了两次同样的错误。1个paren代表strcmp(),1个paren代表if()
您应该包括string.h。此外,您应该将函数f2c和c2f放在main之前
你也写了
prinf
在f之前用t试试
printf
最后你需要
exit(0);
在第一个if之后
if (argc!=3)
{
printf("Incorrect number of arguments");
exit(0);
}
否则,程序的其余部分将运行,您将遇到seg故障。欢迎使用编程。轻微挑剔:
float c2f(float);
float f2c(float);
虽然它在技术上是正确的,但请记住在函数声明中也要包含变量名,这样更易于阅读
例如
float c2f(float c);
我使用了以下代码:
/* Declare Initial library for functions */
#include<stdio.h>
#include<conio.h>
/* Main function*/
void main()
{
/* data type(float),variable(c,f)*/
float c, f;
/* printf function from stdio.h library , for printing level*/
printf("Enter temp. in Celsius: ");
/* scanf for inserting data in variable*/
scanf("%f",&c);
/* Fahrenheit rules*/
f = c * 9/5 + 32;
/* Result will display in this line */
printf("Temp. in Fahrenheit: %f",f);
/* getch function from conio.h library, used to write a character to screen*/
getch();
}
/*声明函数的初始库*/
#包括
#包括
/*主要功能*/
void main()
{
/*数据类型(浮动),变量(c,f)*/
浮点数c,f;
/*来自stdio.h库的printf函数,用于打印级别*/
printf(“输入摄氏温度:”);
/*用于在变量中插入数据的scanf*/
scanf(“%f”、&c);
/*华氏度规则*/
f=c*9/5+32;
/*结果将显示在此行中*/
printf(“华氏温度:%f”,f);
/*来自conio.h库的getch函数,用于将字符写入屏幕*/
getch();
}
谢谢,我没有看到。我更正了它,在main之前添加了string.h、f2c和c2f投影,但出于某种原因,我仍然得到了错误:(@MichaelBlue#include
以及strtod
你重新编译对了吗?:)J/K.那么你得到了完全相同的错误?嘿,Kninnug!我确实包含了#include,但出于某种原因我仍然得到了printf错误:/see my edit。你输入错误。现在将此标记为答案!!!并且更仔细地检查你的代码!Stackoverflow不是用来查找你的打字错误的(但别难过。错误消息对初学者没有帮助,我理解)现在确实是时候阅读警告/错误消息,并在发布之前尝试自己做这项工作。如果出现警告,例如“函数'strcmp'的隐式声明”让你感到困惑的是,发布一个简化的程序,否则看起来你肯定要承担大部分的工作。嘿,chux,我4天前才开始用C编程,我仍然对错误感到困惑。不是你发布的是问题,而是你没有展示你解决问题的能力。而不是po用多个错误重复整个代码,并说“我不知道为什么”,就代码的某个特定方面提出一个具体问题。在停止之前,表明您也尝试过研究并找出答案。我已经研究并完成了整个代码的编写。只是我遇到了一些错误,我不知道为什么会出现这些错误以及如何解决它们。如果您知道如何解决这些错误,请稍后再试x printf错误以及为什么我会得到它,你会非常有帮助。*4天前开始用C编写代码,只是说。这个问题是为了寻求解释,而不仅仅是为了工作代码。你的回答没有为提问者提供任何见解,可能会被删除。请解释观察到的症状的原因。现在这个答案可以了吗?