Hi Kevin,
The problem that you mentioned above can be very easily circumvented by defining a
unique identifier (e.g. a class), and then throwing that. I have rewritten
the code below to show what I mean :
Code:#include <iostream> using namespace std; const int asize = 5; namespace { class unique{ } jump; }; int main(void) { //Method 1 - using goto to exit double for loop for(int row = 0; row < asize; ++row){ for(int col = 0; col < asize; ++col){ cout<<"row = "<<row<<", col = "<<col<<endl; if(row==2 && col==2){ goto escape; } } } escape : cout<<"Finished method 1\n"; //Method 2 - using throw and catch to exit double for loop int escape=0; try{ for(int row = 0; row < asize; ++row){ for(int col = 0; col < asize; ++col){ cout<<"row = "<<row<<", col = "<<col<<endl; if(row==2 && col==2){ throw jump; } } } }catch(unique){} cout<<"Finished method 2\n"; system("pause"); return(0); }




Reply With Quote