Thread: Exception handling

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    Exception handling

    hi

    please can you help me in the following question?

    Write a C++ program that inputs a list of integer values in the range of -100 to 100 from the keyboard and computes the sum of the squares of the input values. This program must use exception handling to ensure that the input values are in range and are legal integers, to handle the error of the sum of the squares becoming larger than a standard int variable can store, and to detect end-of-file and use it to cause output of the result. In the case of overflow of the sum, an error message must be printed and the program terminated.

    can you please give me a hint how to solve this question?
    please set free our POWs

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > can you please give me a hint how to solve this question?
    Start with a board search for "exception" to see how other people have used it

    Which bit of the problem is actually causing you a problem, because at the moment, it's just homework dumped on the board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    This was my solution
    but i don't know how can i detect an overflow??

    Code:
    #include <iostream.h>
    
    int main(){
    	
        int x;
        int eof = 0;
        int sum = 0;
    
        cout<<"Enter a list of integers in the range of -100 to 100 (0 to end):"<<endl;
        cin>>x;
    	
          try{
            if(x<-100 || x>100)
              throw x;
            sum = x*x;}
          catch(int e){
            cout<<e<<" is out of range"<<endl;}
    
          while(x != eof){
            cin>>x;
            try{
              if(x<-100 || x>100)
                throw x;
              sum = sum+(x*x);}
            catch(int e){
              cout<<e<<" is out of range"<<endl;}
    	}
    	
           cout<<"sum of the input squares = "<<sum<<endl;
           return 0;}
    Last edited by kuwait; 12-11-2003 at 05:15 AM.
    please set free our POWs

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but i don't know how can i detect an overflow??
    One way is like this

    Code:
    if ( INT_MAX - sum < x ) {
        // sum + x will be > INT_MAX - overflow will happen if you try it
        // throw exception
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    thanks for your help
    please set free our POWs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:54 AM
  5. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM