# Thread: recursion

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

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

3. Semicolon after the call to powers().

Edit: Ha! I beat Sean this time

4. There's no semi-colon on the line before it.

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

Popular pages Recent additions