finding the max power using recursion

i have this program assignment and one part of it is trying to find the max power a number will go to(x) without exceeding a number the user inputs it not to exceed(y). we are using it in a function. this is the whole program and what i have for max power it just keeps returning 0.


#include <iostream>
#include <cmath>
using namespace std;
// meunue where you can get your options from
void menue(){
cout << "choose the following options:" << endl;
cout << "1) Power of x raised by y." << endl;
cout << "2) Find the max power a number can be raised to." << endl;
cout << "3) Print out a number with its digits in reversed order." << endl;
cout << "4) Sum of integers from 1 to n." << endl;
cout << "5) Product of integers from 1 to n." << endl;
cout << "6) Quit" << endl;
}

//functions for finding the power usign recursion
int Power(int a, int b){
int x=1,i;
for (i=1;i<=b;i++){
if (b==0){
return Power(a,b--);
}
else{
x=x*a;
}
}
return x;
}
int maxpower( int n, int max_value ){
int temp = temp * n;
if( temp > max_value )
return 0 ;
else return maxpower( n, max_value + 1 ) ;
}

int reverse(int number){
int lastDigit, numberOfDigits, sign = 1;//sets the sign equal to one
// if number is less than 0 returns 0
if(number < 0){
return 0;
}
else
//if a number is under 10 than it can not be switched so you times the number by 10 and switch it.
if(number < 10)
return number*sign;

lastDigit = number % 10;
number = number / 10;
numberOfDigits = log10(number) + 1;
//recursive statement that calls the function
return (lastDigit*pow(10,numberOfDigits) + reverse(number))*sign;
}

//finding the sum
int sum (int n){
if(n != 0){
return n + sum (n-1);//recursive statement
}
else{
return n;
}
}

//finding the product
int product (int n){
int temp;
if(n <= 1) {
return 1;
}
else {
temp = n * product (n - 1);// recursive statement setting temp == to recursive statement
return temp;//returning temp
}

}


int main () {
int a;
int x;
int y;
int length = 0;
int temp;
int results;
// calls menue and get prints all the options

do {
menue();
//inserts the choice
cin >> a;

cout << "you choose:" << a << endl;//prints the choice out.
//switch statement that will take account for the number you choose and prints the results
switch (a){
case 1:
cout << "enter the number to raise" << endl;
cin >> x;
cout << " enter the power to raise to: " << endl;
cin >> y;
Power(x,y);
cout << "the result is:" << Power(x,y) << endl;
break;

case 2:
cout << "Enter the number to raise:" << endl;
cin >> x;
cout << "Enter the number not to exceed:" << endl;
cin >> y;
maxpower(x,y);
cout << "the result is:" << maxpower(x,y) << endl;

break;

case 3:
cout << " enter numbers to be reversed by: " << endl;
cin >> x;
temp = x;
while (temp != 0){
length++;
temp = temp / 10;
}
reverse(x);
cout << "the result is:" << reverse(x) << endl;
break;

case 4:
cout << "enter the number to sum to: " << endl;
cin >> x;
sum(x);
cout << "the result is:" << sum(x) << endl;

break;

case 5:
cout << "enter the number to multiply to:" << endl;
cin >> y;
product(y);
cout << "the result is:" << product(y) << endl;
break;
case 6:
cout << "good bye!!" << endl;
break;
}

} while (a!= 6);
return 0;
}
1
2
3
4
5
6
int maxpower( int n, int max_value ){
int temp = temp * n; // Using an uninitialized local variable
if( temp > max_value )
return 0 ;
else return maxpower( n, max_value + 1 ) ;
}


It should be :
1
2
3
4
5
int maxpower( int n, int max_value, int product = 1)
{
    if(static_cast<int>(pow(n, product)) >= max_value) return product;
    return maxpower( n, max_value, product + 1) ;
}
okay thank you but what does that do? i haven't learned about static_cast<int>
i am in just a beginners class so we haven't learned that
Okay thank you but what does that do? i haven't learned about static_cast<int>
I am in just a beginners class so we haven't learned that

http://www.cplusplus.com/doc/tutorial/typecasting/
Didn't your other post, here
http://www.cplusplus.com/forum/general/201496/
solve this problem for you?

If there's something you don't understand about it, why don't you ask in that thread?
Topic archived. No new replies allowed.