Question
I have a recursive power function my instructions are If user enters non integers, then catch the exception and prompt again, how do i do
I have a recursive power function my instructions are If user enters non integers, then catch the exception and prompt again, how do i do this in the read function from main?
int power(int x, int y){ // recursive power funtion reads two int values if(y>= 0){ if (y==0) return 1; else if (y==1) return x; else return x * power(x,y-1); } else{ return 1/power(x,abs(y)); } }
int main(){ bool success=false; do { try { // read the input values, throw if non int read(); success=true; } catch(...) { // catching all types of exceptions cout << "error in data, try again" << endl << endl; } } while (!success); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started