0

Im trying to eliminated the infinite loop by using exception handling in my code but its not working can someone take a look at this snippet and tell me what I'm doing wrong?

void Addition(float num)
 {
  
  cout<<"Please enter number you wish to add:"<<endl;
  cin>>num;
  
  while( num!=-9999)
  {
   Sum+=num;
   cout<<"Sum is:"<<Sum<<endl;
   cout<<"Please enter number or enter -9999 to exit"<<endl;
   try{
    cin>>num;
   }
   catch(...)
   {cout<<"ERROR"<<endl;
   }
  
   
  }
  
  
 }
peta23
  • 11
  • 1
  • 4
  • 1
    Add `num=-9999;` at the end, but inside, the catch block. Or just print out a message that says if you want to exit enter -9999 – doug Dec 04 '16 at 02:10
  • The exception is caught inside the while loop, and execution simply continues. If you want the exception to terminate the `while` loop, you must have the try/catch outside of the loop. The entire `while` loop must be inside the `try` block. – Sam Varshavchik Dec 04 '16 at 02:10
  • Do try and avoid using *magic numbers* like this. What if someone needed to add `-9999` to the sum? That's what the `eof()` signal is for. – tadman Dec 04 '16 at 02:24
  • This code doesn't throw any exceptions. Invalid input puts `std::cin` into an error state which you can test. – Pete Becker Dec 04 '16 at 02:45
  • @PeteBecker, good point. His code doesn't show use of an exception mask and the default is not to throw. – doug Dec 04 '16 at 03:20
  • @tadman how do I use eof() to end my program instead of -9999 I'm new at this – peta23 Dec 07 '16 at 23:09
  • There's [lots of answers in other questions](http://stackoverflow.com/questions/201992/how-to-read-until-eof-from-cin-in-c) on the subject. Have a poke around. – tadman Dec 07 '16 at 23:13

1 Answers1

1

exceptions are only caught if they are thrown first. No part of your code throws any exception using the throwkeyword. But even if you do that your catch block has to be in the loop as well so you have to exit loop using break statement if that's your intention

You can do the following

void Addition(float num)
    {
        int Sum=0;
        cout<<"Please enter number you wish to add:"<<endl;
        cout<<"Please enter number or enter -9999 to exit"<<endl;

        while( true ) // whcih actually makes it infinite
        {
            try{
                cin>>num;
                if(num == -9999) 
                     throw -9999; // you can throw any value in this case
                Sum+=num;
            }
            catch(...)
            {
             cout <<" ABORTING.."<<endl;
             break;
            }   
        }

       cout << "Sum is:" << Sum << endl;
    }

The above code is quite unnecessary it could have done simply like this

void Addition(float num)
        {
            int Sum=0;
            cout<<"Please enter number you wish to add:"<<endl;
            cout<<"Please enter number or enter -9999 to exit"<<endl;

            while( true ) // whcih actually makes it infinite
            {

                    cin>>num;
                    if(num == -9999) 
                    {
                       cout << "ABORTING.." << endl;
                       break;
                    }

                    Sum+=num;
            }

            cout << "Sum is:" << Sum << endl;
        }
Syed Ahmed Jamil
  • 1,881
  • 3
  • 18
  • 34