What is the bug here?

Write a C++ function that get two numbers from the user and displays the result of first number raise to the power of second number using do while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>

using namespace std;

int main() {
int n,p,r;
int counter = 1;
cout << "Enter " << endl;
cin >> n>>p;

do {
    r = n*1;
    counter++;
}
while (counter <= p);
cout << r;

}  

nvm i figured out myself
I was supposed to initialization the value of r to one and
at line 13 I was supposed to write

r=r*n;
you still have to put all that into a function

your code should be in this form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>

using namespace std;

void power_func();

int main() {
power_func();

}

void power_func()
{
    int n,p,r;         ///let your variables be descriptive
    int counter = 1;
    cout << "Enter " << endl; ///use a descriptive message
    cin >> n>>p;

    do {
           r = n*1;   ///this is wrong should be like num1 *= num2
           counter++;
         }
     while (counter <= p);
     cout << r;  

}


Ty
Topic archived. No new replies allowed.