Firstly I'd like to thank the creators of this website, it has the best tutorials I could find anywhere!
I just started learning C++ as my first programming language with no prior knowledge of anything to do with programming whatsoever, so I'm sorry if this question sounds stupid.
I don't know how to use powers in C++ yet, but I decided to build something to solve powers just for practice. Here's what I have:
#include <iostream>
usingnamespace std;
int power (int x, int y)
{
int z=0;
do
{
x*=x;
z++;
}
while (z!=y);
return (x);
}
int main()
{
int a, b;
cout << "enter a value\n";
cin >> a;
cout << "enter a second value\n";
cin >> b;
int c;
c = power (a,b);
cout << '\n' << c;
}
I know this isn't "really" solving powers and it won't work for anything to the power of 1 or below, but like I said I'm doing this for practice. For some reason, this isn't working and I can't figure out why.
If y is less than zero , then that's like doing 1/pow(x,+y). You won't be able to give an accurate answer without truncating if you're only using ints for this.