Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Little help here for a noob?

Name: orzo 2007-11-02 22:23

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: orzo 2007-11-02 22:26

I own 4Chan! I AM Anonymous! That's right, suck on that!

Name: Anonymous 2007-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?

Name: Anonymous 2007-11-02 22:35

>>2

Fine, Ill stay anon if it really pisses you off that much.

>>3

The powered function, it just comes up with that regular "This program has stopped working correctly and needs to close" message.

Name: Anonymous 2007-11-02 22:56

No shit. You're going into an infinite loop. Needs to be if (b==1), not a.
And for fuck's sake, use a loop instead of recursion. This isn't Lisp.

Name: Anonymous 2007-11-02 22:59

>>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.

Name: Anonymous 2007-11-02 23:01

>>6

GTFO

Name: Anonymous 2007-11-02 23:11

>>5

Thanks a ton, that fixed it.

Name: Anonymous 2007-11-02 23:12

>>5
I think most Lispers would have the sense to use a loop here. Schemers, on the other hand....

Name: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-11-02 23:41

>>11
Dear god your indentation makes my eyes bleed.

Name: Anonymous 2007-11-02 23:42

>>1
learn how to spell 'answer' correctly.  or, if you must insist on misspelling it, at least be consistent.

Name: Anonymous 2007-11-02 23:43

>>11
You can tune a car, but you can't tune a cdr.

Name: Anonymous 2007-11-02 23:55

>>12
oh I copied the style from a /prog/ coder. I like it, it's nice for oneliners.

Name: Anonymous 2007-11-02 23:57

>>15
I invented that style.

Name: Anonymous 2009-03-06 13:23

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.

Don't change these.
Name: Email:
Entire Thread Thread List