编程C语言温度转换

时间:2014-08-06 22:42:21

标签: c temperature

我刚刚开始编程,这是我的第一个任务之一。我真的不明白为什么这段代码不起作用。任何帮助将不胜感激

#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[])
{

    float temp;
    int result1;
    char  scale;
    char convert;
    char ch1 = 'F';
    char ch2 = 'f';
    char ch3 = 'C';
    char ch4 = 'c';
    char ch5 = 'K';
    char ch6 = 'k';

    float result;

    printf(" Please Enter Temperature: ");
    scanf("%f", &temp);
    printf("Scale is: ");
    scanf("%s", &scale);


    if(scale == 'F' || scale == 'f')
    {
        printf("convert to: ");
        scanf("%s", &convert);


        if(convert == 'c' || convert == 'C')
        {
            result = (temp-32)*(5/9);
            printf("original temperature: %f\n", temp);
            printf("scale conversion: %s to %s\n", scale, convert); 

在这里出现错误,它说“格式指定类型'char *'但参数的类型为'char'??

            printf("result: %f\n", result);
        }

else
    if(scale == 'C' || scale == 'c')
    {
        printf("convert to:");
        scanf("%s", &convert);


        if(convert == 'f' || convert == 'F')
        {
            result= ((temp*1.8)+32);
            printf("original temperature: %f\n", temp);
            printf("scale conversion: %s to %s\n", scale, convert);
            printf("result: %f\n", result);

        }
else
    if(scale == 'K' || scale == 'k')
    {
        printf("convert to:");
        scanf("%s", &convert);

        if(convert == 'C' || convert == 'c')
        {
            result=(temp+273);
            printf("original temperature: %f\n", temp);
            printf("scale conversion: %s to %s\n", scale, convert);
            printf("result: %f\n", result);
        }
else
    if (scale == 'C' || scale == 'c')
    {
        printf("convert to:");
        scanf("%s", &convert);

        if(convert == 'K' || convert == 'k')
        {
            result=(temp-273);
            printf("original temperature: %f\n", temp);
            printf("scale conversion %s to %s\n", scale, convert);
            printf("result: %f\n", result);
        }
else
    if (scale == 'F' || scale == 'f')
    {
        printf("convert to:");
        scanf("%s", &convert);

        if (convert == 'K' || convert == 'k')
        {
            result=((temp+459)*0.55);
            printf("original temperature: %f\n", temp);
            printf("scale conversion %s to %s\n", scale, convert);
            printf("result: %f\n", result);
        }
else
    if (scale == 'K' || convert == 'k')
    {
        printf("convert to:");
        scanf("%s", &convert);

        if (convert == 'F' || convert == 'f')
        {
            result=(0.55*(temp-273)+32);
            printf("original temperature: %f\n", temp);
            printf("scale conversion %s to %s\n", scale, convert);
            printf("result: %f\n", result);
        }
    }
    }
    }
    }

    }
    }

    printf("\nthank you so much for using this program...press any key to exit\n");
    getchar();

    return 0;
}

1 个答案:

答案 0 :(得分:3)

%s格式说明符仅用于以空值终止的字符串(类型为char*),而不是单个字符(char)。对于单个字符,请使用%c