【发布时间】:2017-10-18 12:29:36
【问题描述】:
Kelvin 到 C 或 F 的转换程序,它使用我们实验室的函数。我不知道为什么我一直得到 0 作为输出。我认为这与我传递/返回值的方式有关。我可能还没有得到回报?
#include <stdio.h>
#include <ctype.h>
float KtoC(float kel)
{
kel = - 273;
return(kel);
}
float KtoF(float kel)
{
kel = (9 * kel / 5) + 32;
return(kel);
}
float GetKelvin(float kel)
{
printf("Temperature Conversion Program\n");
printf("Please enter a temperature in Degrees Kelvin:");
scanf("%f%*c", &kel);
return(kel);
}
char GetConvType(char &ctype)
{
do
{
printf("Convert to Celcius (c) or Fahrenheit (f): ");
scanf("%c%*c", &ctype);
ctype = tolower(ctype);
} while(ctype != 'c' && ctype != 'f');
return(ctype);
}
float GetConvTemp(float kel)
{
char ctype;
ctype = GetConvType(ctype);
if(ctype == 'c')
{
return (KtoC(kel));
}
else
{
return (KtoF(kel));
}
}
void DisplayResults(float x)
{
printf("This is the temp converted %f", x);
return;
}
int main()
{
float kel, x;
char ctype;
GetKelvin(kel);
GetConvType(ctype);
GetConvTemp(kel);
DisplayResults(kel);
return(0);
}
【问题讨论】:
-
因为你没有使用
GetKelvin(kel);的返回值 -
多么奇怪的程序编写方式
-
请注意,
char GetConvType(char &ctype)是 C++,而不是 C。 -
@FedericoklezCulloca 是的,我知道...很久没干了,我们从 C 开始。
-
顺便说一下,KtoC 总是返回 -273。您应该使用
kel -= 273而不是kel = -273。
标签: c++ parameter-passing return-value modular