无法找出为什么我的显示值始终为0

时间:2017-05-18 15:32:01

标签: c++ parameter-passing return-value modular

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);

}

1 个答案:

答案 0 :(得分:0)

--- xorg.cpp    2017-05-19 00:48:56.000000000 +0900
+++ x.cpp   2017-05-19 00:50:23.000000000 +0900
@@ -4,19 +4,19 @@

 float KtoC(float kel)
 {
-   kel = - 273;
+   kel -= 273.0;

    return(kel);
 }

 float KtoF(float kel)
 {
-   kel = (9 * kel / 5) + 32;
+   kel = (9.0 * KtoC(kel) / 5.0) + 32.0;

    return(kel);
 }

-float GetKelvin(float kel)
+float GetKelvin(float &kel)
 {
    printf("Temperature Conversion Program\n");
    printf("Please enter a temperature in Degrees Kelvin:");
@@ -39,7 +39,7 @@
    return(ctype);
 }

-float GetConvTemp(float kel)
+float GetConvTemp(float &kel)
 {
    char ctype;

@@ -71,8 +71,7 @@

    GetKelvin(kel);
    GetConvType(ctype);
-   GetConvTemp(kel);
-   DisplayResults(kel);
+   DisplayResults(GetConvTemp(kel));

    return(0);