Thread: recursion

  1. #16
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Code:
    int powers(int x; unsigned int y) {
        if(y==0) {
            cout<<"1";//any base to the power of 0 is 1
            return 1; }
        else {
        powers(x*x(y-1)); }
    
    }
    Your old version was only putting cout<<"1"; in the if statement and calling return 1; no matter what. Also, this is more personal, but not putting brackets around code in if statements is ugly and evil. *Shudder*

    Edit: Both Sean and Thantos beat me. D'oh!
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    int main()
    {
        char op='c';
        int x,y;
        while (op!='e')
        {
        cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), Powers(^) (e)exit?";
        cin>> op;
        switch(op)
        {
        case '^':
        cout<<"Enter the the base then power"<<endl;
        cin>>x;
        cin>>y;
        cout<<powers(x,y)
        break;
    in function 'int main()'
    syntax error before 'break'

    Could you please tell me how to fix these problems?
    My computer is awesome.

  3. #18
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Semicolon after the call to powers().

    Edit: Ha! I beat Sean this time
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  4. #19
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    There's no semi-colon on the line before it.

  5. #20
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Yay it works!
    This is all of my code if you care.
    Code:
    #include <iostream>
    
        int multiply(int x,int y)
        {
        return x*y;
        }
        
        int powers(int x, unsigned y)
        {
        if(y==0)
        return 1;
        else
        return(x*powers(x, y-1));
        }
        
        int divide(int x, int y)
        {
        return x/y;
        }
        
        int add(int x, int y)
        {
        return x+y;
        }
        
        int subtract(int x, int y)
        {
        return x-y;
        }
    
    using namespace std;
    
    int main()
    {
        char op='c';
        int x,y;
        while (op!='e')
        {
        cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), Powers(^) (e)exit?";
        cin>> op;
        switch(op)
        {
        case '^':
        cout<<"Enter the the base then power"<<endl;
        cin>>x;
        cin>>y;
        cout<<powers(x,y)<<endl;
        break;
    
        case '+':
        cout<<"Enter the numbers you would like to add."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
        break;
        
        case'-':
        cout<<"Enter the numbers you would like to subtract."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
        break;
        
        case'/':
        cout<<"Enter the numbers you would like to divide."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"/"<<y<<divide(x,y)<<endl;
        break;
        
        case'*':
        cout<<"Enter the numbers you would like to multiply."<<endl;
        cin>>x;
        cin>>y;
        cout<<x<<"*"<<y<<multiply(x,y)<<endl;
        break;
        case'e':
        return 0;
        default:
        cout<<"sorry, try again."<<endl;
        }
        }
        
        return 0;
    }
    Thanks for the help.
    My computer is awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM