in this code although i have a general catch block but when i try to enter a double value instead of integer one the program runs infinitely what to do???
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
usingnamespace std;
void main(){
while(1){ try{
cout<<"Enter the dividend ";
int x;
cin>>x;
cout<<"Enter the divisor ";
int y;
cin>>y;
if(y==0)
throw 0;
int ans;
ans = x
/y;
cout<<"\nDivision gives result : "<<ans<<endl;
}
catch(int){
cout<<"\n\ndivisor wrongly entered as 0 re enter please\n\n";
}
catch(...){
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
What I do first is to indent the code to make it readable.
I believe entering a double into an int yields undefined behaviour, so we promote int to double.
i want to say like we are entering values into an int type of variable and the user enters a double type value than we prompt our user that you have entered a wrong value and you should enter an integer value....