Functions

Sharky Forums


Results 1 to 4 of 4

Thread: Functions

  1. #1
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    Functions

    #include<iostream>
    using namespace std;
    void PowerFunc(int, int);
    void main()
    {
    int x, y;
    cout << "Enter two ints: ";
    cin >> x >> y;
    PowerFunc(x,y);
    }
    //************************************************************ ****
    void PowerFunc(int a, int b)
    {
    int sum = 1;
    cout << "The number " << a << " is to be raised "
    << " the power of " << b <<endl;

    for(int x = 1; x <= b; x++)
    {
    sum *= a;
    x++;
    }
    //cout << x <<endl;
    cout << "The answer is " << sum << endl;
    }
    //************************************************************ ****
    When two ints are entered and passed to the PowerFunc, it gives me the two numbers I entered but it doesn't give me the correct answer. It shows with the cout<<x<<endl; statement that it goes through the loop 5 times and not 4. Here is a sample output:

    Enter two ints: 2 4
    The number 2 is to be raised to the power of 4
    The answer is 4

    What am I doing wrong?

  2. #2
    Tiger Shark
    Join Date
    Feb 2001
    Location
    Satan Country
    Posts
    564
    You are incrementing x in the loop declaration, and again inside the loop.

    You should probably take the one in the loop out.
    I'm half Scottish and half French.

    I surrender to alcohol.

  3. #3
    Catfish f15e's Avatar
    Join Date
    Mar 2002
    Location
    I live in the south.
    Posts
    114

    function

    Damn, what the hell was I thinking? Apparantly I wasn't! Sometimes the obvious is invisible. Must be some magic act. haha
    Thanks Bryce777

  4. #4
    Tiger Shark
    Join Date
    Feb 2001
    Location
    Satan Country
    Posts
    564

    Re: function

    Originally posted by f15e
    Damn, what the hell was I thinking? Apparantly I wasn't! Sometimes the obvious is invisible. Must be some magic act. haha
    Thanks Bryce777
    Yeah, I have written a million of those things, and once in a while I still do something retarded like put a semicolon after the ).

    ouch!
    I'm half Scottish and half French.

    I surrender to alcohol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •