Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do i change this so its a built in function? #include <iostream> #include <c

ID: 3553826 • Letter: H

Question

How do i change this so its a built in function?


#include <iostream>

#include <cstdlib>

using namespace std;


void readnums (int& b, int& e) {

int correctInput;

cout << "Enter the base and the exponent:";

cin>>b>>e;

if (!cin) {cout<<"Disaster! Terminating program."<<endl;

exit (-1);

}

correctInput=(b>=0)&&(e>=0);

while (!correctInput){

cout<<"Something wrong! Try again..."<<endl;

cout<<"Re-enter base and exponent:",

cin>>b>>e;

if(!cin){

cout<<"Disaster! Terminating program."<<endl;

exit (-1);

}

correctInput = (b>=0)&&(e>=0);

}

}


int exp (int b, int e){

int result;

result= 1;

while (e!=0){

result=result*b;

e=e-1;

}

return (result);

}

int main ()

{

int x, y;

readnums (x, y);

cout<<exp(x,y)<<endl;

return 0;

}

Explanation / Answer

#include <iostream>

#include <cstdlib>

using namespace std;

int main ()

{

int x, y;

int &b = x ;

int &e = y ;

int correctInput;

cout << "Enter the base and the exponent:";

cin>>b>>e;

if (!cin) {cout<<"Disaster! Terminating program."<<endl;

exit (-1);

}

correctInput=(b>=0)&&(e>=0);

while (!correctInput){

cout<<"Something wrong! Try again..."<<endl;

cout<<"Re-enter base and exponent:",

cin>>b>>e;

if(!cin){

cout<<"Disaster! Terminating program."<<endl;

exit (-1);

}

correctInput = (b>=0)&&(e>=0);

}

int result;

result= 1;

while (e!=0){

result=result*b;

e=e-1;

}


cout << result << endl ;

return 0 ;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote