+ 2

Need some help doing somethin in c++

Im trying to make a program to make expoent math but i cant do it for some reason, Here is the code int expoent(int b, int e){ long result = b; if (e > 1){ for (int i = e; i <= 1; i--){ result *= b; return result; } }else if (e = 1){ return result; } } int main() { int c; int f; cout << "input base" << endl; cin >> c; cout << "input expoent" << endl; cin >> f; int z = expoent (c,f); cout << "the result is " << z;

26th Jan 2018, 1:16 PM
diogo Neutel
diogo Neutel - avatar
6 Respostas
+ 1
Right after the closing bracket of for loop.
26th Jan 2018, 1:35 PM
Harjeet Singh
Harjeet Singh - avatar
+ 2
thanks very much ir works now
26th Jan 2018, 1:37 PM
diogo Neutel
diogo Neutel - avatar
+ 1
1. function expoent return type is int, but you're returning long. 2. in for loop, condition "i<=1", should be "i>=1". 3. return result, shouldn't be inside for loop otherwise it'll end the function in the first iteration only. 4. also takes care of power 0, result should be 1.
26th Jan 2018, 1:26 PM
Harjeet Singh
Harjeet Singh - avatar
+ 1
in the loop condicionado since Im doing i -- the condicionado should be <= 1
26th Jan 2018, 1:29 PM
diogo Neutel
diogo Neutel - avatar
+ 1
Imagine, user wants the result of, 2 raise to power 3. Then in for loop, first value of i will be 3, which is greater than 1 and the condition will be false, so for loops will not be executed. So the condition should be, "i>=1", to be exact it should be "i>1".
26th Jan 2018, 1:32 PM
Harjeet Singh
Harjeet Singh - avatar
+ 1
oh ok and the returno should be where?
26th Jan 2018, 1:34 PM
diogo Neutel
diogo Neutel - avatar