请帮我修复这个程序。我需要知道丢失了什么?它在我的“扫描”和“如果”行上给我错误。告诉我,我有“未初始化的变量?我真的不确定如何纠正它!”
#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
答案 0 :(得分:2)
更改
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");
到
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);
在if(enter==f)
中,f
未初始化
(此代码有太多错误!)
答案 1 :(得分:2)
用于c
和f
的变量是什么?您在比较中使用它们,但您从不为它们分配任何东西。
根据您的代码逻辑,我假设您要检查用户是否输入了字符 '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
答案 2 :(得分: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