   failtbit set
  Hi, im new to coding and have recently bought what i think is a helpfull book for a n00b  (SAMS Teach Yourself C++ in 10 minutes 2nd edition... It was the only C++ book available in my town, seems alright though :/)Im gradually grasping some of the language but have become stuck at a certain point. in main.cpp int main(int argc, char* argv[]){SAMSErrorHandling::Initialise(); //call the funtctiondo{ try {  char Operator = GetOperator();  float Operand = GetOperand();  cout << Accumulate(Operator,Operand) << endl; }  catch (runtime_error RuntimeError) { SAMSErrorHandling::HandleRuntimeError(RuntimeError); }  catch (...) {  SAMSErrorHandling::HandleNotANumberError(); }}while (SAMSPrompt::UserWantsToContinueYOrN("More? "));return 0;} in ErrorHandlingModule.cpp #include <iostream>#include <exception>#include "ErrorHandlingModule.h"namespace SAMSErrorHandling{using namespace std;void Initialise( void ){cin.exceptions( cin.failbit );}int HandleNotANumberError( void ) // Returns Error code{cerr << "Input error - not a number?" << endl;cin.clear();char BadInput[5];cin >> BadInput;return 1;}int HandleRuntimeError( runtime_error theRuntimeError ){cerr << theRuntimeError.what() << endl;return 1;}}in ErrorHandlingModule.h   #ifndef ErrorHandlingModuleH#define ErrorHandldingModuleHusing namespace std;namespace SAMSErrorHandling{void Initialise(void);int HandleNotANumberError( void );// Returns error codeint HandleRuntimeError( runtime_error theRuntimeError );}#endifI didn't paste the entire modules code, just the stuff i "think" matters...The first catch works perfectly, returning the error message when i don't type a legit operator.But when i don't type a legit operand in, like a letter, instead of printing the correct error message i get "ios::failbit set" and an abnormal program error.EDIT : I pasted the entire errormodule.cpp, sorry for posting so much code.[edited by - ozzo on February 5, 2004 1:16:55 AM]
  Put your code in a [source] box, or few will have the patience to figure how the nesting works out.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  Show the code for GetOperator(), GetOperand() and UserWantsToContinueYOrN().Is it code you have copied from the book? If so, check carefully for typos, such as a forgotten & and the like.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  located in main.cpp char GetOperator(void) // returns dividend, char and no argument{char Operator;cout << "Operator: ";cin >> Operator;return Operator; // returns a copy of Operator}float GetOperand(void) // returns divisor, float and no argument{float Operand = 1;cout << "Operand: ";cin >> Operand;return Operand; // returns a copy of Operand}and in promptmodule.cpp #include <iostream>#include "PromptModule.h"namespace SAMSPrompt{using namespace std;void PauseForUserAcknowledgement( void ){//Must type something before pressing 'Enter'char StopCharacter;cout << endl << "Press a key and \"Enter\": ";cin >> StopCharacter;}bool UserWantsToContinueYOrN( const char *theThingWeAreDoing){char DoneCharacter;bool InvalidCharacterWasEntered = false;do{cout <<endl <<theThingWeAreDoing <<" - Press \"n\" and \"Enter\" to stop: ";cin >> DoneCharacter;InvalidCharacterWasEntered =! ((DoneCharacter == 'y') || (DoneCharacter == 'n'));if (InvalidCharacterWasEntered){cout << "...Error -" << "please enter \"y\" or \"n\"." << endl;}}while (InvalidCharacterWasEntered);return (DoneCharacter != 'n'); // true when not "n"}} [edited by - ozzo on February 5, 2004 8:16:56 PM];  Ok. So does the error happen within the exception handler or outside of it ? If it happens within the handler, I'd rewrite it this way: #include <limits>int HandleNotANumberError( void ) // Returns Error code{  cerr << "Input error - not a number?" << endl;  cin.exception(ios_base::goodbit);  cin.clear();  cin.ignore(numeric_limits<streamsize>::max(), '\n');  cin.exceptions(ios_base::failbit);  return 1;}In your code, if cin >> BadInput fails, an exception is raised and, since you're already in an exception handler, the program is terminated (you can only have one pending exception at any given time).Furthermore, you should always catch exceptions by reference, never by value.catch (runtime_error& RuntimeError){  SAMSErrorHandling::HandleRuntimeError(RuntimeError);}  “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 5, 2004 8:46:02 PM]