Thread: CIN and exception handling

  1. #1
    Registered User OldSchool's Avatar
    Join Date
    Apr 2006
    Location
    Virginia
    Posts
    18

    CIN and exception handling

    Hi there... I have an assignment this week in an OOP class and the program already demonstrates simple exception handling according to the assignment requirements. This question is more for my own benefit... I had a problem with the proper use of the failbit a few weeks ago:
    Failbit trouble
    I received some great assistance and thought that I had this concept down so I used a similar section of code to detect non-int values for this assignment. When the program is run, the program catches decimal values and any other inputs consisting of a number followed by anything non-int. However, the user hitting a letter or and non-numeric character first results in an untimely exit...

    There is more to the code including another throw and catch for long number errors but I removed the parts that were not pertinent to this problem... Suffice to say, I used #includes to avoid having to type std:: and the actual throw constructors are outside of main and seem to work fine...

    Code:
    int main()
    {
        int num;    // user-specified ID 
    	cout << "Enter a positive integer ID number (max 6-digit) for validation" <<
    		"\n(end-of-file to end): ";
    
        // user ID input
    		while ( cin >> num ) {
    
    		try {
    			// Tests input for invalid data format
    			if ( cin.fail() || cin.get() != '\n' ) {
    				cin.clear();
    				cin.ignore(5000,'\n');
    				throw InvalidFormatException();
    		        } // end if
    
    			} // end try block
    
    		// exception handler handles invalid format exceptions
    		catch ( InvalidFormatException &InvalidFormatException ) {
    			cout << "\nAn exception occurred: " <<
      			InvalidFormatException.what() << endl;
    
    		} // end catch
    
    		cout << "\nEnter a positive integer ID number (max 6-digit) for validation" <<
    			"\n(end-of-file to end): ";
    
    	}  // end while
    
    	cout << endl;
    
    	return 0;  // terminate normally
    
    }  // end main
    I am stumped as to what is happening here... I thought that a non-numerical input would result in the failbit setting and throwing the exception... Instead, all I get is 'Press any key to continue' and POOF!

    From what I understood after reading the thread I listed above and this one , it appears to me that this type of input should be caught with that 'if' check...

    Any assistance would be appreciated... Thanks in advance...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Of course the exception won't be thrown, the loop terminates immediately if cin>>num fails, and the try..catch is inside of the loop.
    My best code is written with the delete key.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    while ( cin >> num )
    That line above is the point where your input is read. And when it's invalid, it breaks out of the loop right away. Your if-statement is never reached when failed reading occurs.

    edit: damn, my internet connection is slow nowadays
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User OldSchool's Avatar
    Join Date
    Apr 2006
    Location
    Virginia
    Posts
    18
    Thanks, Prelude... That did the trick!

    I just switched to a 'for' loop for repetition instead of the 'while' and moved my 'cin >> num' inside of the loop... This way, exception can be processed and the user can try another value...

    That one was kicking my butt but seemed so easy after I read your post! I guess I just had a case of 'dain-bramage'...

    Thanks again!

  5. #5
    Registered User OldSchool's Avatar
    Join Date
    Apr 2006
    Location
    Virginia
    Posts
    18
    Thanks alphaoide... I appreciate the reply... I just fixed my code... Works great now with any input I throw at it!

    I don't know why I didn't think of this! I feel kind of stupid on this one...

    Sometimes it's the little things that kill... LOL!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Unhandled Exception
    By MasterOfTheBass in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 01:45 AM