Input Failure Error ... Problem

This is a discussion on Input Failure Error ... Problem within the C++ Programming forums, part of the General Programming Boards category; My program seems to be skipping some steps when executing. I will post the code first, and then explain the ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Question Input Failure Error ... Problem

    My program seems to be skipping some steps when executing. I will post the code first, and then explain the details.

    Code:
    /* This program acts as a calculator -- a very basic one.
    ** It asks the end-user for two numbers and the operation to be performed
    ** then gets the dirty job over with.
    */
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {	
    	char operation;
    	double num1, num2, result;
    	
    	cout << "Enter the first number: ";
    	cin >> num1;
    	
    	if (!cin)
    	{
    		cout << "Input Failure Error: the number must either be"
    			 << " an integer or a decimal." << flush;
    		return 1;
    	}
    	
    	cout << "Enter the second number: ";
    	cin >> num2;
    	
    	if (!cin)
    	{
    		cout << "Input Failure Error: the number must either be"
    			 << " an integer or a decimal." << flush;
    		return 1;
    	}
    	
    	cout << "Enter the operation to be performed:"
    		 << "\n[a] for addition"
    		 << "\n[s] for substraction"
    		 << "\n[m] for multiplication"
    		 << "\n[d] for division" 
    		 << "\n>_" << flush;
    	
    	cin >> operation;
    	
    		// This is the code that is getting skipped
    	if (!cin)
    	{
    		cout << "There has been an input failure error:"
    			 << " you must enter a character value."
    			 << "\nThis application will now exit." << endl;
    		return 1;
    	}// End problematic code here
    	
    	cout << fixed << showpoint << setprecision(2) << endl;
    	
    	switch (operation)
    	{
    		case 'a':
    		case 'A':
    			result = num1 + num2;
    			cout << "The sum of " << num1 << " and " << num2
    				 << " is " << result << endl;
    			break;
    		
    		case 's':
    		case 'S':
    			result = num1 - num2;
    			cout << "The difference between " << num1 << " and " << num2
    				 << " is " << result << endl;
    			break;
    		
    		case 'm':
    		case 'M':
    			result = num1 * num2;
    			cout << "The product of " << num1 << " and " << num2
    				 << " is " << result << endl;
    			break;
    			
    		case 'd':
    		case 'D':
    			if (!num2 == 0)
    			{
    				result = num1 / num2;
    				cout << "The quotient of " << num1 << " and " << num2
    					  << " is " << result << endl;
    			}
    			else cout << "You can't divide by zero, stupid!" << endl;
    			break;
    		
    		default:
    			cout << "The value entered does not match any option offered,"
    				 << " \nor capability possessed by this application. Exiting.";
    	}// switch ends here.
    	
    	return 0;
    }
    Now to those details: the program compiles and execute fine ... only problem is that the code from line 46 to 52 gets skipped if I try to force an input failure error at line 43 (e.g.: enter a string or integer instead of a character for the "operation" variable) and goes directly to the default output of the switch structure at line 91.

    Any help as to why this is happening would be very much appreciated. Thanks in advance.

    --Alphonse.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    You can't enter an anything that isn't a character, bud.

    Look at your keyboard. It's all characters. '1', 'a', '+', ESC. Everything is a character. You'll never get an input failure on that cin statement. Entering a string does nothing but leave extra characters in your input buffer to be taken (or rejected) by the next cin statement.

    Now, to your real problem. How, oh how do you make sure it's between a-z and A-Z. Well take a look at an ascii chart and compare your inputted character to the ascii values of the alphabet.

    http://www.lookuptables.com
    Last edited by SlyMaelstrom; 02-24-2006 at 06:22 AM.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    How about that?! I am going to go bang my head on the wall now. Thank you.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Lastly, giving an error message and exiting the program makes for kind of a dull and non user-friendly program. Consider using your extraction statement in a loop condition. That way, it will reprompt for input every time it gets bad input.

    Code:
    while (!(cin >> num2)) {
       cin.clear();           // Sets your failed stream flag back to working
       cin.ignore(100,'\n');  // Empties your input buffer
       cout << "Error, invalid input.\nPlease re-enter: ";
    };
    See that? Rinse and repeat.
    Last edited by SlyMaelstrom; 02-24-2006 at 06:26 AM.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21