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 ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.