Sunday, August 2, 2009

C++ problem?

Why does this one give me an application error?





#include%26lt;conio.h%26gt;


#include%26lt;stdio.h%26gt;


#include%26lt;iostream.h%26gt;





int i,a,exp;


int rek(int n);





main()


{


clrscr();


cout%26lt;%26lt;"Give in base: "; cin%26gt;%26gt;a;


cout%26lt;%26lt;"Give in exponent: "; cin%26gt;%26gt;exp;


cout%26lt;%26lt;"Result is "%26lt;%26lt;rek(exp);


getch();


return(0);


}





int rek(int n)


{


return((exp==0)?1:rek(n-1)*a);


}

C++ problem?
%26gt; return((exp==0)?1:rek(n-1)*a);


................^^^





Did you mean (n==0)? exp isn't shrinking, only 'n' is.





You probably hit a "stack overflow" because the recursive calls never stop.





This error is likely due to the fact that you're referencing other global variables, which is bad practice and hides the dependency.


No comments:

Post a Comment