Need help identifying error in function

double expression(int n, double x)
{
double num=(pow((x),2*a));
double den=fact(n,a);

double sum=0;
for(int a=0; a>=0; a++)
sum=pow(x,n)*(num/den);
return sum;
}

I don't know why this isn't working. fact is a factorial function which I have tested and it works fine.
but when I input, for example
int main()
{
cout<<expression(1,3)<<endl;

getch();
return 0;
}

it comes up with an error.
can anyone see what i am doing wrong?
it comes up with an error.

What error? Why would you withhold useful information from us?
That's the thing I don't know. It just says build unsuccessful. Normally if something is wrong there is a red line under what is wrong or it says something useful in the build bit at the bottom. But it just says "build unsuccessful"
:/
could it possibly be the infinite for loop?
Added code tags. See my comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double expression(int n, double x)
{
    double num=(pow((x),2*a));     // 'a' is undefined here
    double den=fact(n,a);    // and here

    double sum=0;
    for(int a=0; a>=0; a++)     // infinite loop
        sum=pow(x,n)*(num/den);
    return sum;
}

int main()
{
    cout<<expression(1,3)<<endl;

    getch();
    return 0;
}


Edit: The build errors will be there somewhere. Where they are depends on your IDE
Last edited on
Assuming you're including all the necessary headers, these are the compile errors I got with your code in ideone.com.

prog.cpp: In function ‘double expression(int, double)’:
prog.cpp:7:24: error: ‘a’ was not declared in this scope
  double num=(pow((x),2*a));
                        ^
prog.cpp:8:21: error: ‘fact’ was not declared in this scope
  double den=fact(n,a);
                     ^



a is only defined locally in your for loop block.
Last edited on
Topic archived. No new replies allowed.