-5

请帮我修复这个程序。我需要知道缺少什么?它在我的“扫描”和“如果”行上给了我错误。告诉我我有“未初始化的变量?我真的不知道如何纠正它!

#include<stdio.h>

int main (void)
{
//Local Declarations
int c;
int f;
int cel;
int fah;
int enter;
//Statements

printf("\nThis program converts Celsius temperatire to Fahrenheit degree and Fahrenheit         temperature to Celsius degree.");
printf("\nIf you want to convert Celsius to Fahrenheit, please enter c.");
printf("\nIf you want to convert Fahrenheit to Celsius, please enter f.");
scanf("%d, &enter");

if(enter==c)
{
printf("\nEnter Celsius temperature.");
enter=cel;
    scanf("%d", &cel);
fah=cel*5/9 + 32;
printf("%d", fah);
}

if(enter==f)
{
printf("\nEnter Fahrenheit degree.");
    enter=fah;
    scanf("%d", &fah);
cel=(fah-32)*5/9;
printf("%d", cel);
}

return 0;


} //main
4

3 回答 3

2

Change

printf("\nIf you want to convert Celsius to Fahrenheit, please enter c.");
printf("\nIf you want to convert Fahrenheit to Celsius, please enter f.");
printf("\nIf you want to convert Fahrenheit to Celsius, please enter f.");
scanf("%d, &enter");

to

printf("\nIf you want to convert Celsius to Fahrenheit, please enter c.");
scanf("%d", &c);
printf("\nIf you want to convert Fahrenheit to Celsius, please enter f.");
scanf("%d", &f);
printf("\nIf you want to convert Fahrenheit to Celsius, please enter f.");
scanf("%d", &enter);

In if(enter==f), f is not initialized.
(This code has too many errors!!)

于 2013-10-14T14:50:54.570 回答
2

变量cf用途是什么?你在比较中使用它们,但你从不为它们分配任何东西。

根据您的代码逻辑,我假设您要检查用户是否输入了字符 'c''f'. 如果是这种情况,您将需要进行以下更改:

char enter;
...
scanf(" %c", &enter); // leading space in format string is important; %c won't skip
                      // leading whitespace by itself, so if you don't want to 
                      // accidentally capture a newline or other whitespace character,
                      // you need to have the leading space before the conversion
                      // specifier.
...
if (enter == 'c')     // compare enter to the *value* 'c', not the variable c
  // process celcius

if (enter == 'f')     // same as above, but for 'f'
  // process fahrenheit
于 2013-10-14T14:59:03.923 回答
0

在逗号前结束引号。逗号为您的类型转换占位符提供参数,例如整数值的 %d。

%d 是接受输入或打印出整数

我认为解决你的任务是错误的,但你去吧。

    int main (void)
    {
    //Local Declarations
    int cel;
    int fah;
    char enter;
    //Statements

    printf("Gerald Onesky COP2220 Project 3");
    printf("\nThis program converts Celsius temperatire to Fahrenheit degree and Fahrenheit  temperature to Celsius degree.");
    printf("\nIf you want to convert Celsius to Fahrenheit, please enter c.");
    printf("\nIf you want to convert Fahrenheit to Celsius, please enter f.");
    scanf("%c, &enter");

    if(enter=='c')
    {
        printf("\nEnter Celsius temperature.");        
        scanf("%d", &cel);
        fah=cel*5/9 + 32;
        printf("Celsius temperature is %d and Fahrenheit temperature is %d", cel, fah);
    }

    if(enter=='f')
    {
        printf("\nEnter Fahrenheit degree.");
        scanf("%d", &fah);
        cel=(fah-32)*5/9;
        printf("Celsius temperature is %d and Fahrenheit temperature is %d", cel, fah);
    }    
    return 0;   
  } //main
于 2013-10-14T14:57:56.440 回答