English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Here is my code


[code]
#include
#include
using namespace std;

double factorial(double n)
{
factorial=n;

while (n>1)
{
factorial=(factorial*(n-1));
n--;
}
return factorial;
}

int main()
{
double n;
double factorial;

cout<<"Enter the value for which you want its factorial: \n";
cin>>n;

cout<<"The factorial is "<
return 0;
}
[/code]

I know the factorial function is right because when I include it in main() it works on its own, but referencing it as a function is causing some error problems:

error C2659: '=' : function as left operand

error C2296: '*' : illegal, left operand has type 'double (__cdecl *)(double)'

error C2440: 'return' : cannot convert from 'double (__cdecl *)(double)' to 'double'. There is no context in which this conversion is possible

error C2064: term does not evaluate to a function taking 1 arguments.

Any ideas as to what I did wrong?

2007-02-26 06:46:00 · 4 answers · asked by ? 2 in Computers & Internet Programming & Design

4 answers

When referencing this function, have you previously created/included a function prototype?

EDIT: Oh, duh - "factorial" is not a global variable. You have not defined the variable type for "factorial" within the function factorial().

Hint: instead of factorial=n; write: double factorial=n;

2007-02-26 06:49:08 · answer #1 · answered by greeneyedprincess 6 · 1 0

I think you are confusing the compiler by trying to use the same name "factorial" both for the function that takes a double and returns a double, AND also as a local variable.

In main, you don't even need a local variable called factorial, so you can delete the line

double factorial;

In the factorial function, I would add a declaration

double answer;

Then replace lines like

factorial=n

with

answer = n

and so on. That way "answer" is your local variable, which is declared as such and is distinct from "factorial" the function, which is defined above main.

2007-02-26 15:06:38 · answer #2 · answered by Johnny 2 · 2 0

double factorial(double n)
{
factorial=n;

Honey, look at what you are doing. You have a FUNCTION named "factorial". Now in the first line of that function, you are trying to assign a double value "n" to that FUNCTION. That's wrong in about 10 different ways. The first two errors you are getting are two of the ways in which this is wrong. You can't have a function on the left side of an assignment statement. And even if you could do that, the types on the left and right side of your assignment statement don't match.

2007-02-26 14:52:54 · answer #3 · answered by Lisa A 7 · 2 0

You really got some very good answers already. I would just like to add a thing. In your function factorial, why are you assigning the value of n to factorial?
You can replace factorial with n itself, as n is a local variable to that function.
Hope this helps a bit!
Good Luck!

2007-02-28 03:12:07 · answer #4 · answered by A.Samad Shaikh 2 · 0 0

fedest.com, questions and answers