C温度转换程序保持输出0华氏摄氏 [英] C Temperature Conversion Program Keeps Outputting 0 For Fahrenheit to Celsius

查看:184
本文介绍了C温度转换程序保持输出0华氏摄氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C我的温度转换程序保持输出0,当我试图把华氏转换为摄氏温度。摄氏度到华氏度转换似乎工作就好了。我也做了同样的事情为功能和部分,但我一直在第二次转换得到0。是否有人可以帮助我,或告诉我什么,我做错了什么?

 的#include<&stdio.h中GT;//函数声明浮get_Celsius(浮点*摄氏度); //获取要转换的摄氏温度值。
无效to_Fahrenheit(浮点CEL); //摄氏值转换华氏并打印新的价值。
浮get_Fahrenheit(浮点*华氏度); //获取要转换的华氏温度值。
无效to_Celsius(浮点FAH); //华氏价值为摄氏转换并打印新的价值。INT主要(无效)
{
   //本地声明
   华氏浮动;
   浮摄氏度;
   浮动;
   浮动B:   //声明
   的printf(请输入在摄氏温度值转换为华氏:\\ n);
   A = get_Celsius(安培;摄氏度);
   to_Fahrenheit(一);
   的printf(请在华氏输入的温度值转换为摄氏:\\ n);
   B = get_Fahrenheit(安培;华氏度);
   to_Celsius(二);   返回0;
} //主浮get_Celsius(浮点*摄氏度)
{
   //声明
   scanf函数(%F,&安培; *摄氏度);
   返回*摄氏度;
}无效to_Fahrenheit(浮点CEL)
{
   //本地声明
   浮动FAH;   //声明
   FAH =((CEL * 9)/ 5)+ 32;
   的printf(华氏温度为:%F \\ N,FAH);
   返回;
}浮get_Fahrenheit(浮点*华氏度)
{
   //声明
   scanf函数(%F,&安培; *华氏度);
   返回*华氏;
}无效to_Celsius(浮点FAH)
{
   //本地声明
   浮CEL;   //声明
   CEL =(FAH-32)*(5/9);
   的printf(摄氏温度为:%F \\ N,CEL);
   返回;
}


解决方案

  CEL =(FAH-32)*(5/9);

在这里, 5/9 是整数除法,其结果是 0 ,将其更改为 5.0 / 9


而在几行,您使用

  scanf函数(%F,&安培; *摄氏度);

&放大器; * 是没有必要的,只要 scanf函数(%F,摄氏); 会怎么做。

My temperature conversion program in C keeps outputting 0 when I attempt to convert Fahrenheit to Celsius. The conversion from Celsius to Fahrenheit seems to work just fine. I have done the exact same thing for both functions and portions but I keep getting 0 for the second conversion. Can someone please help me or tell me what I am doing wrong?

#include <stdio.h>

//Function Declarations

float get_Celsius (float* Celsius);       //Gets the Celsius value to be converted.
void to_Fahrenheit (float cel);           //Converts the Celsius value to Fahrenheit and prints   the new value.
float get_Fahrenheit (float* Fahrenheit); //Gets the Fahrenheit value to be converted.
void to_Celsius (float fah);              //Converts the Fahrenheit value to Celsius and prints the new value.

int main (void)
{
   //Local Declarations
   float Fahrenheit;
   float Celsius;
   float a;
   float b;

   //Statements
   printf("Please enter a temperature value in Celsius to be converted to Fahrenheit:\n");
   a = get_Celsius(&Celsius);
   to_Fahrenheit(a);
   printf("Please enter a temperature value in Fahrenheit to be converted to Celsius:\n");
   b = get_Fahrenheit(&Fahrenheit);
   to_Celsius(b);

   return 0;
} //main

float get_Celsius (float* Celsius)
{
   //Statements
   scanf("%f", &*Celsius);
   return *Celsius;
}

void to_Fahrenheit (float cel)
{
   //Local Declarations
   float fah;

   //Statements
   fah = ((cel*9)/5) + 32;
   printf("The temperature in Fahrenheit is: %f\n", fah);
   return;
}

float get_Fahrenheit (float* Fahrenheit)
{
   //Statements
   scanf("%f", &*Fahrenheit);
   return *Fahrenheit;
}

void to_Celsius (float fah)
{
   //Local Declarations
   float cel;

   //Statements
   cel = (fah-32) * (5/9);
   printf("The temperature in Celsius is: %f\n", cel);
   return;
}

解决方案

cel = (fah-32) * (5/9);

Here, 5/9 is integer division, its result is 0, change it to 5.0/9


And in several lines, you are using

scanf("%f", &*Celsius);

&* is not necessary, simply scanf("%f", Celsius); would do.

这篇关于C温度转换程序保持输出0华氏摄氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆