#include <iostream.h>
int Doubler(int number, int power);
int main ()
{
int number;
int power;
int newnumber;
cout <<"Please enter a number to work with.\n";
cin >> number;
cout << "Now please enter a power.\n";
cin >> power;
newnumber = Doubler(number, power);
cout <<"The number with the power equals" << newnumber;
return 0;
}
int Doubler(int number, int power)
{
if (power ==1)
return number;
else
return (number * Doubler(number, power--));
}
weird error message
Hi, I'm using VC++6 and when I type in the following code I get an illegal operation message when I type in the power of the number. This is really weird and has never happened before. Any ideas of what's happening?
thanks
[edited by - viper35al on April 15, 2002 11:50:57 AM]
Not sure about your problem, but what if I sent in 0 as the value for power?, or -2 for that matter?
You are using postfix decrement when you recurse into Doubler. That means power will not be decremented until after recursion, so you''ll never hit the termination condition and the program runs away with the stack. If you change it to prefix decrement, you should be fine.
I also noticed you are using iostream.h. You should really be using iostream (without a ".h"), which will require adding a "using namespace std;" after the header declarations.
I also noticed you are using iostream.h. You should really be using iostream (without a ".h"), which will require adding a "using namespace std;" after the header declarations.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement