Thread: Input Failure Error ... Problem

1. 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[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. 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

3. How about that?! I am going to go bang my head on the wall now. Thank you.

4. 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.