Power Functions

Hi I am having trouble using the power function for integers, I need the result to be an integer number. I'm trying to get the user to type in both the values of the base and the exponent but I keep getting an error that says:

ambiguous call to overloaded function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
using namespace std;

int main(){

int base=0, exponent=0, power =0;
cout<<"Enter base\n";
cin<<base;
cout<<"Enter exponent\n"
cin<<exponent;

power= (int) pow((int) base, (int)exponent);
cout<<power;

return 0;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

    int base = 0, exponent = 0, power = 0;
    cout << "Enter base\n";
    cin >> base; //switched the << to a >>
    cout << "Enter exponent\n"; //You forgot a semi-colon
    cin >> exponent; //switched the << to a >>

    power = (int) pow((int) base, (int)exponent);
    cout << power;

    return 0;

}


The comments should show you want went wrong :)
Thanks those were minor problems but the main problem is still my power function. I think it's something to do with them being ints.
Last edited on
Your original code that you posted, on lines 9 and 11, have cin <<
EDIT:

What doesn't work? It works fine for me.
Last edited on
Sorry I wasn't trying to come off as upset. But it still doesn't work, I use Microsoft Visual Studios, it keeps giving me an error that my 'pow' is ambiguous to overloaded function
I think you need to change the int base and int exponent and int power to a double.
ie; double base, double exponent, double power.

Also, shouldnt line 13 be written: power= pow(base, exponent);

But what if I want the result to come out as an integer? Is there any way I could go about doing that? Or is it not possible for me to use the power function. And I did try that but it gives me the same error
You also need cin.ignore().get(); before the return to keep the console screen from disappearing,
ie as a system replacement. Then you can view the result.
Are you talking about about a breakpoint? I used that and it keeps the console screen from disappearing.
A double is an integer, it just gives you a decimal fraction.
A breakpoint is a very odd way to keep the console open. Use:

1
2
3
4
5
    //code
    std::cout << "Press ENTER to exit\n";
    std::cin.get();
    return 0;
}


instead. Other options are available: linked by Alpha (are we competing here? :D )
Last edited on
This works fine for me ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
using namespace std;

int main(){

double base, exponent;
double power =0;
cout<<"Enter base\n";
cin>> base;
cout<<"Enter exponent\n";
cin >> exponent;

power= pow(base, exponent);
cout<<power;

cin.ignore().get();
return 0;

}
My prof told me to use a breakpoint. But I am new at c++, so I was wondering does it matter what program I use to run it into? What are you guys using because I am using Microsoft Visual Studios and it still gives me that error
Im using MS 2010 express. The code I posted above works fine.
Are you sure youve changed your original line 13 to power= pow(base, exponent);??
Have you compiled many other programs in MVS? If not, or other problems of this nature have occurred, then reinstalling wouldn't hurt. Otherwise, I am at a loss. I am running it fine in Code::Blocks.

If you were referring to how to keep the console open, yes, you can put that code into any program to keep it from closing. That way, if you want to simply double click the executable, you can without it closing early.
The problem for me is I need to keep those identifiers as ints, I can't change them into doubles because I am making a program that's a lot more complex than that and it requires them to be ints. Is there any way I could use the power function and keep those as ints?
Last edited on
I 'm not sure that 'pow' function will accept just int (as indicated by the initial error).

Anyway, as I posted before a double / float is the same as an integer with the difference being that they accept decimal values where as int does not. IE:

int 1.2 + int 1.1 = 2

whereas

double 1.2+ double 1.1 = 2.3.
Last edited on
Topic archived. No new replies allowed.