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.