I'm just starting C++ and I'm having the worst time getting this little thing I wrote to run. It's supposed to take two variables from the user, one as a number and another to multiply it to the power of. It compiles fine and everything but when it's supposed to call the function it crashes. Anyone have any ideas why this is? Thanks in advance.
#include <cstdlib>
#include <iostream>
using namespace std;
unsigned int powered(unsigned int, unsigned int);
int main ()
{
unsigned int number, power, anwser;
cout << "Enter a number: ";
cin >> number;
cout << "Enter a power: ";
cin >> power;
anwser = powered(number,power);
cout << "The awnser is: " << anwser << endl;
char response;
cin >> response;
return 0;
}
unsigned int powered(unsigned int a, unsigned int b)
{
if(a == 1)
return a;
else
return (a * powered(a,b-1));
}
Name:
orzo2007-11-02 22:26
I own 4Chan! I AM Anonymous! That's right, suck on that!
Name:
Anonymous2007-11-02 22:27
>>1
Either this is your homework and we don't want to do it for you, or you're learning C++ voluntarily and you need to stop it now.
When what is supposed to call what function it crashes with what error message?
>>2
It's the Retard Vigilante! :O
Nobody gives a shit, really. Forced anon only applies to /b/.
In fact, when you're asking a question it's better to use a name, to avoid confusion when more information is needed.
>>5
I think most Lispers would have the sense to use a loop here. Schemers, on the other hand....
Name:
Anonymous2007-11-02 23:25
Since I'm bored:
unsigned int powered (unsigned int a, unsigned int b) {
unsigned int c = 1;
for (int i = 0; i < b; i++)
c *= a;
return c;
}
This has the added benefit of dealing with x^0 correctly.
Name:
Anonymous2007-11-02 23:32
>>9
which loop? in lisp, you would use (loop), but in c, you don't have such a facility. This is cooler:
unsigned int
powered
(unsigned int a, unsigned int b)
{ return b == 0 ? 1 : a * powered(a,b-1); }
Name:
Anonymous2007-11-02 23:41
>>11
Dear god your indentation makes my eyes bleed.
Name:
Anonymous2007-11-02 23:42
>>1
learn how to spell 'answer' correctly. or, if you must insist on misspelling it, at least be consistent.
Name:
Anonymous2007-11-02 23:43
>>11
You can tune a car, but you can't tune a cdr.
Name:
Anonymous2007-11-02 23:55
>>12
oh I copied the style from a /prog/ coder. I like it, it's nice for oneliners.
The first snowstorm makes the single front door rather impracticable and everyone flees across the street They turn into a mess after a few of the different Linux.