In the following program:
int main(){
std::cout<<"enter numbers to be divide"<<std::endl;
int a,b,c;
while(true){
try{
if(!(std::cin>>a>>b)){
throw std::invalid_argument("please enter proper interger");
}
if(b==0){
throw std::runtime_error("please enter enter nonzero divisor");
}
c=a/b;
std::cout<<"quotient = "<<c<<std::endl;
}
catch(std::invalid_argument e){
std::cout<<e.what()<<"\ntry again?enter y/n";
char c;
std::cin>>c;
if(c=='n'||c=='N') break;
}
catch(std::runtime_error e){
std::cout<<e.what()<<"\ntry again?enter y/n";
char c;
std::cin>>c;
if(c=='n'||c=='N') break;
}
}
return 0;
}
I am using two kinds of exception.Program is working perfectly when it throws "runtime_error" exception but goes into infinite loop when encounter "invalid_argument" exception. Actually there is problem in "cin>>c
" statement in catch-block but can not figure out, why this is happening.