This question already has an answer here:
这个问题已经有了答案:
- Why am I getting a 'called object 0 is not a function error'? 4 answers
- 为什么我得到一个所谓的0不是一个函数错误?4答案
I'm running into the following error in both functions in the following code:
在以下代码中,我在两个函数中都遇到了以下错误:
In function 'Celsius_Converter':
Line 64: error: called object '0' is not a function
In function 'Fahrenheit_Converter':
Line 90: error: called object '1' is not a function
Here is my code:
这是我的代码:
/*
*Program Name: COP 2220-10018 Project 3
*
* Author: Nathan Gamble
*
* Description: Convert either Celsius to Fahrenheit, or Fahrenheit to Celsius.
*
* Input: Celsius or Fahrenheit Temperature
*
* Output: Fahrenheit or Celsius, depending on which one the User requested.
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
//Local Declarations
char choice;
int fahrenheit;
int celsius;
//Statements
printf("This program converts Celsius temperature to Fahrenheit degree and Fahrenheit temperature to Celsius degree. \n");
printf("If you want to convert Celsius to Fahrenheit, please enter C.");
printf("If you want to convert Fahrenheit to Celsius, please enter F.");
scanf("%s", &choice);
//Process
if (choice == 'C') {
fahrenheit = Celsius_Converter();
printf("The temperature in Fahrenheit degree is %d", &fahrenheit);
}
else {
celsius = Fahreinheit_Converter();
printf("The temperature in Celsius degree is %d", &celsius);
}
return 0;
} // End main
/*
*Program Name: COP 2220-10018 Project 3
*
* Author: Nathan Gamble
*
* Description: Convert Celsius to Fahrenheit
*
* Input: Celsius Temperature
*
* Output: Fahrenheit
*/
int Celsius_Converter ()
{
//Local Declarations
double fahrenheit;
double celsius;
printf("Enter a temperature in Celsius.");
scanf("%d", &fahrenheit);
celsius = (5/9) (&fahrenheit - 32);
return celsius;
} //end Celsius_Converter
/*
*Program Name: COP 2220-10018 Project 3
*
* Author: Nathan Gamble
*
* Description: Convert Fahrenheit to Celsius
*
* Input: Fahrenheit Temperature
*
* Output: Celsius
*/
int Fahrenheit_Converter ()
{
//Local Declarations
double fahrenheit;
double celsius;
printf("Enter a temperature in Fahrenheit.");
scanf("%d", &celsius);
fahrenheit = (9/5) (&celsius + 32);
return fahrenheit;
} //end Fahrenheit_Converter
2 个解决方案
#1
4
You have a few problems here:
这里有几个问题:
-
You're using the address of some values instead of the values theselves. You use the address of
celsius
when you callscanf
(which is right, becausescanf
needs to know where to store the value):您使用的是一些值的地址,而不是这些值。当您调用scanf时,您使用摄氏度的地址(这是正确的,因为scanf需要知道在哪里存储值):
double celsius; scanf("%d", &celsius);
but you need to
celsius
itself when you do arithmetic, not its address:但是当你做算术时,你需要的是摄氏度,而不是它的地址:
fahrenheit = (9/5) (celsius + 32); // instead of &celsius
There's a similar error in the other conversion routine, where you use
&fahrenheit
rather thanfahrenheit
. This is also an issue in your output. When you're printing, you need to use the value of the variable, not its address. So在其他转换程序中也有类似的错误,使用的是华氏温度而不是华氏温度。这也是输出中的一个问题。当您打印时,您需要使用变量的值,而不是它的地址。所以
printf("The temperature in Celsius degree is %d", &celsius);
should also use
celsius
, not&celsius
.也应该用摄氏度,而不是摄氏度。
-
Once you've got those worked out, though, you can address your main issue.
不过,一旦你搞定了这些,你就可以解决你的主要问题了。
something (...)
is function call syntax in C. This means that when you do, e.g.,
(9/5)(celsius + 32)
, you're trying to call a function(9/5)
, which is, by integer arithmetic,0
. So, you need to make the multiplication explicit, using*
, so you'd have在c中是函数调用语法,这意味着当你做的时候,例如(9/5)(摄氏度+ 32),你试着调用一个函数(9/5),也就是,通过整数运算,0。所以,你需要使用*,使乘法更显式。
(9/5)*(celsius+32)
but then you'll still run into the problem that
9/5
is1
, because it's integer arithmetic. You can fix this by making at least one of those numbers a non-integer, e.g., by using(9/5.0)
:但是你仍然会遇到9/5是1的问题,因为它是整数运算。您可以通过将这些数字中的至少一个作为非整数来解决这个问题,例如,通过使用(9/5.0):
fahrenheit = (9/5.0) * (celsius + 32);
A similar explanation holds for the other case, except that
5/9
is0
.另一种情况也有类似的解释,但5/9是0。
-
The return types of some of your methods aren't right. E.g., since you're trying to return a
double
, the function needs to be declared to return adouble
, not anint
.有些方法的返回类型不正确。例:既然您试图返回double,则需要声明函数返回double,而不是int。
int Celsius_Converter () // needs to be double { double celsius; ... celsius = (5/9) (&fahrenheit - 32); return celsius; } //end Celsius_Converter
#2
0
First, the answer to your question:
首先,回答你的问题:
(9/5) (&celsius + 32)
is a function invocation, more precisely, you are trying to invoke the function 9/5, which is 1, passing &celsius + 32 (which is an error) as its parameter. What you want to do is probably (9/5) * (&celsius + 32)
. There is no such thing as implicit operators in C. Actually, it should be (9/5) * (celsius + 32)
: the & is wrong here. The same goes for the other function.
(9/5)(&摄氏度+ 32)是一个函数调用,更确切地说,您试图调用函数9/5,它是1、通过和摄氏度+ 32(这是一个错误)作为参数。你想做的可能是(9/5)*(和摄氏度+ 32)。在c中不存在隐式运算符,实际上,它应该是(9/5)*(摄氏度+ 32):这里的&是错误的。另一个函数也是这样。
Then, some other considerations on your code:
然后,在你的代码上还有一些其他的考虑:
-
scanf("%s", &choice);
is a hazard, because you are asking for buffer overruns. You'd better doscanf("%c", &choice);
orscanf(“% s”,选择);是一种危险,因为您要求缓冲区溢出。你最好选择scanf(“%c”,&choice);或
char choice[2]; ... scanf("%1s", choice);
-
printf("The temperature in Fahrenheit degree is %d", &fahrenheit);
is wrong. It should beprintf("The temperature in Fahrenheit degree is %d", fahrenheit);
. The same goes for Celsius. The & is needed forscanf
s, when you're reading into a primitive type (you'll learn the reason behind this as you go further with your studies, or you can post another question, or check a online C guide).printf(“华氏温度为%d”,华氏温度);是错误的。它应该是printf(“华氏温度是%d”,华氏温度);摄氏度也是一样。当你读到一种原始类型的时候(你会在你的研究中进一步学习,或者你可以发布另一个问题,或者检查一个在线C指南),你需要的是scanfs。
Moreover, I have some doubts on the rationale behind your decomposition in functions, but, again, this is something you don't learn now.
此外,我对您在函数中分解的基本原理有一些疑问,但是,同样,这是您现在不知道的东西。
#1
4
You have a few problems here:
这里有几个问题:
-
You're using the address of some values instead of the values theselves. You use the address of
celsius
when you callscanf
(which is right, becausescanf
needs to know where to store the value):您使用的是一些值的地址,而不是这些值。当您调用scanf时,您使用摄氏度的地址(这是正确的,因为scanf需要知道在哪里存储值):
double celsius; scanf("%d", &celsius);
but you need to
celsius
itself when you do arithmetic, not its address:但是当你做算术时,你需要的是摄氏度,而不是它的地址:
fahrenheit = (9/5) (celsius + 32); // instead of &celsius
There's a similar error in the other conversion routine, where you use
&fahrenheit
rather thanfahrenheit
. This is also an issue in your output. When you're printing, you need to use the value of the variable, not its address. So在其他转换程序中也有类似的错误,使用的是华氏温度而不是华氏温度。这也是输出中的一个问题。当您打印时,您需要使用变量的值,而不是它的地址。所以
printf("The temperature in Celsius degree is %d", &celsius);
should also use
celsius
, not&celsius
.也应该用摄氏度,而不是摄氏度。
-
Once you've got those worked out, though, you can address your main issue.
不过,一旦你搞定了这些,你就可以解决你的主要问题了。
something (...)
is function call syntax in C. This means that when you do, e.g.,
(9/5)(celsius + 32)
, you're trying to call a function(9/5)
, which is, by integer arithmetic,0
. So, you need to make the multiplication explicit, using*
, so you'd have在c中是函数调用语法,这意味着当你做的时候,例如(9/5)(摄氏度+ 32),你试着调用一个函数(9/5),也就是,通过整数运算,0。所以,你需要使用*,使乘法更显式。
(9/5)*(celsius+32)
but then you'll still run into the problem that
9/5
is1
, because it's integer arithmetic. You can fix this by making at least one of those numbers a non-integer, e.g., by using(9/5.0)
:但是你仍然会遇到9/5是1的问题,因为它是整数运算。您可以通过将这些数字中的至少一个作为非整数来解决这个问题,例如,通过使用(9/5.0):
fahrenheit = (9/5.0) * (celsius + 32);
A similar explanation holds for the other case, except that
5/9
is0
.另一种情况也有类似的解释,但5/9是0。
-
The return types of some of your methods aren't right. E.g., since you're trying to return a
double
, the function needs to be declared to return adouble
, not anint
.有些方法的返回类型不正确。例:既然您试图返回double,则需要声明函数返回double,而不是int。
int Celsius_Converter () // needs to be double { double celsius; ... celsius = (5/9) (&fahrenheit - 32); return celsius; } //end Celsius_Converter
#2
0
First, the answer to your question:
首先,回答你的问题:
(9/5) (&celsius + 32)
is a function invocation, more precisely, you are trying to invoke the function 9/5, which is 1, passing &celsius + 32 (which is an error) as its parameter. What you want to do is probably (9/5) * (&celsius + 32)
. There is no such thing as implicit operators in C. Actually, it should be (9/5) * (celsius + 32)
: the & is wrong here. The same goes for the other function.
(9/5)(&摄氏度+ 32)是一个函数调用,更确切地说,您试图调用函数9/5,它是1、通过和摄氏度+ 32(这是一个错误)作为参数。你想做的可能是(9/5)*(和摄氏度+ 32)。在c中不存在隐式运算符,实际上,它应该是(9/5)*(摄氏度+ 32):这里的&是错误的。另一个函数也是这样。
Then, some other considerations on your code:
然后,在你的代码上还有一些其他的考虑:
-
scanf("%s", &choice);
is a hazard, because you are asking for buffer overruns. You'd better doscanf("%c", &choice);
orscanf(“% s”,选择);是一种危险,因为您要求缓冲区溢出。你最好选择scanf(“%c”,&choice);或
char choice[2]; ... scanf("%1s", choice);
-
printf("The temperature in Fahrenheit degree is %d", &fahrenheit);
is wrong. It should beprintf("The temperature in Fahrenheit degree is %d", fahrenheit);
. The same goes for Celsius. The & is needed forscanf
s, when you're reading into a primitive type (you'll learn the reason behind this as you go further with your studies, or you can post another question, or check a online C guide).printf(“华氏温度为%d”,华氏温度);是错误的。它应该是printf(“华氏温度是%d”,华氏温度);摄氏度也是一样。当你读到一种原始类型的时候(你会在你的研究中进一步学习,或者你可以发布另一个问题,或者检查一个在线C指南),你需要的是scanfs。
Moreover, I have some doubts on the rationale behind your decomposition in functions, but, again, this is something you don't learn now.
此外,我对您在函数中分解的基本原理有一些疑问,但是,同样,这是您现在不知道的东西。