【问题标题】:C SIMPLE Temperature converter / Celsius to Fahrenheit UNKNOWN errors & outputC SIMPLE 温度转换器/摄氏到华氏未知错误和输出
【发布时间】:2013-10-16 22:33:09
【问题描述】:

前几天刚开始用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 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);
}

另外,我希望我的输出是这样的:

**> 温度转换器 ->f 10.0

10.00°C = 50.00°F**

这应该将 10C 转换为 F。

对于 F 到 C,输出应该是:

温度转换器 ->c 50.0

50.00°F = 10C**

【问题讨论】:

  • 现在是时候阅读警告/错误消息并在发布到 SO 之前尝试自己完成工作了。如果诸如“函数‘strcmp’的隐式声明”之类的警告让您感到困惑,请发布一个简化的程序,否则看起来您肯定是在让 SO 承担大部分工作。
  • 嘿 chux,我 4 天前才开始用 C 编程,但我仍然对错误感到很困惑。
  • 这不是你在发布问题,而是你没有展示你解决问题的能力。与其发布带有多个错误的整个代码并说“我不知道为什么”,不如询问有关代码选择方面的具体问题。表明您在 SO 停留之前也曾尝试研究并找出答案。
  • 我已经研究并完成了我的整个代码的编写。只是我遇到了一些错误,我不知道为什么会遇到这些错误以及如何解决它们。如果您对如何修复 printf 错误以及我为什么得到它有任何想法,您将非常有帮助。
  • *4天前开始用C编码,只是说。

标签: c converter temperature


【解决方案1】:

错误是 if (!strcmp(argv[1], "->f")

它缺少最后一个括号,应该是

if (!strcmp(argv[1], "->f"))

你犯了两次同样的错误。 strcmp() 1 个括号,if() 1 个括号

您应该包含 string.h。另外,你应该把你的函数 f2c 和 c2f 放在 main 之前。

你也写了

prinf

尝试在 f 之前加一个 t

printf

你终于需要了

exit(0);

在第一个 if 之后。例如

if (argc!=3)
{
    printf("Incorrect number of arguments");
    exit(0);
}

否则程序的其余部分会运行,你会得到 seg 错误。欢迎编程。

【讨论】:

  • 谢谢,我没看到。我更正了它,在 main 之前添加了 string.h、f2c 和 c2f 投影,但由于某种原因我仍然收到错误:(
  • @MichaelBlue #include &lt;stdlib.h&gt; 以及strtod
  • 你重新编译对了吗? :) J/K。所以你得到了完全相同的错误?
  • 嘿Kninnug!我确实包含了 #include 但由于某种原因我仍然收到 printf 错误:/
  • 查看我的编辑。你打错了。现在将此标记为答案!并更仔细地检查您的代码! Stackoverflow 不是用来查找拼写错误的(但不要难过。错误消息对初学者没有帮助,我理解)
【解决方案2】:

有点吹毛求疵:

float c2f(float);
float f2c(float);

虽然它在技术上是正确的,但请记住在函数声明中也包含变量名。它使阅读更容易。

举个例子

float c2f(float c);

【讨论】:

    【解决方案3】:

    I used this code :

    /* 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();
        }
    

    【讨论】:

    • 这个问题是在寻找一个解释,而不仅仅是为了工作代码。您的回答没有为提问者提供任何见解,可能会被删除。请edit解释导致观察到的症状的原因。
    • 现在这个答案可以了吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多