【发布时间】:2015-02-13 07:11:15
【问题描述】:
为什么这段代码不起作用?我的 IDE 是 Visual Studio 2013。
#include <stdio.h>
float tempatureGuide(float F, float C);
#define FREEZING_PT 32.0f
#define SCALE_FACTOR (5.0f/9.0f)
int main(void)
{
float fahrenheit = 0.0;
float celsius = 0.0 ;
int convertTemp;
printf ("Enter 0 to calculate Celsius or 1 to calculate Fahrenheit: ");
scanf ("%d", &convertTemp);
if (convertTemp == 0)
{
// compute Celsius
printf("Enter Fahrenheit temperture: ");
scanf("%f", &fahrenheit);
celsius = ((fahrenheit - FREEZING_PT) * SCALE_FACTOR);
printf("Fahrenheit = %f and Celsius = %f\n", fahrenheit, celsius);
float tempatureGuide(fahrenheit, celsius); // Error here
}
else
{
// compute fahrenheit
printf("Enter the temperature in degrees fahrenheit\n\n");
scanf("%f", &fahrenheit);
celsius = (SCALE_FACTOR)* (fahrenheit - FREEZING_PT);
printf("The converted temperature is %f", celsius);
float tempatureGuide(fahrenheit, celsius); // and here
}
return (0);
}
float tempatureGuide(float F, float C){
if (F < 32 || C < 0)
printf("It is freezing!");
else if (F <= 60 || C <= 16)
printf("It is cold");
else if (F >= 70 || C >= 21)
printf("It is just right");
else if (F >= 82 || C >= 28)
printf("It is warm");
else if (F > 95 || C > 35)
printf("It is hot");
else
printf("Please enter a number!");
return (0);
}
这里的目标是添加到我之前所做的转换温度项目中,并向其中添加一个 if else 语句函数,该函数在 temp 上进行 cmet。我得到的错误是
Error 3 error C2078: too many initializes
在我调用函数的两条线上。我搜索了答案,但找不到任何答案。
【问题讨论】:
-
修正缩进。而
float tempatureGuide(fahrenheit, celsius);并不是你调用函数的方式。 -
我已从您的问题中删除了一些不相关的文字。我还缩进了一些代码,所以它显示为代码——但代码布局仍然急需改进。 一致缩进使代码更易于阅读。错误信息中有错字;您应该将确切的错误消息复制并粘贴到您的问题中。 C 和 C++ 是两种不同的语言;你用的是哪一个?您的代码看起来可能同样有效,但错误消息让我认为您正在使用 C++ 编译器编译它。如果您正在编写 C 语言,请将您的 IDE 配置为编译 C。
-
请注意,100 C 或 212 F 的温度将被报告为“恰到好处”,因为您已从
<或<=切换到>=。 -
感谢您的反馈,以后我会尽量缩进。我使用的是 C 而不是 C++ 将我的编译器更改为 C。
标签: c++ c function compiler-errors