这是有问题的代码:
double cf_converter(double t){
//This function converts from celsius to farenheit
if (t <= 200 && t >= -200){
printf("0.00 C ==> 32.00 F");
return CFR*t+32.00;
}
else{
printf("Invalid Farenheit Temperature\n");
return pow(t,3);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的函数是编译器告诉我发生错误的地方。我看过其他示例,但无法确定为什么会收到错误消息。根据编译器,该错误显然是在第一个return语句中发生的,它读取的是CFR * t + 32.00。
void main(){
//Main Loop
char c;
double t, o, input;
printf("Please enter F or C: ");
scanf("%c", &c);
switch(c){
case 'c':
case 'C':
printf("\nPlease enter a celsius degree number: ");
scanf("%lf", t);
o = cf_converter(t);
break;
case 'f':
case 'F':
printf("\nPlease enter a farenheit degree number: ");
scanf("%lf", t);
o = fc_converter(t);
break;
default:
printf("\nThat input is unknown.");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
上面是我目前编写的主要功能。有一个fc_converter()函数与cf_converter函数相同,除了return语句略有不同。我将stdio.h和math.h用于某些功能(例如pow(t,3))。
在回答问题时,CFR如下所示:
#define CFR = 1.8
Run Code Online (Sandbox Code Playgroud)
更改此:
#define CFR = 1.8
Run Code Online (Sandbox Code Playgroud)
对此:
#define CFR 1.8
Run Code Online (Sandbox Code Playgroud)
因为等号在语法上不正确。
此外,更改此:
scanf("%lf", t);
Run Code Online (Sandbox Code Playgroud)
对此:
scanf("%lf", &t);
Run Code Online (Sandbox Code Playgroud)
因为f是类型double。与完全相同的原理char c;,您已经正确传递了scanf调用的地址。
PS:main()在C和C ++中应该返回什么? int不是void。