编程中的runtime_error问题
前几天在编程中,代码如下:
头文件:ErrorHandlingModule.h
//filename:ErrorHandlingModule.h
#ifndef ErrorHandlingModule_H
#define ErrorHandlingModule_H
#include <stdexcept>
using namespace std;
namespace SAMSErrorHandling {
void Initialize(void);
int HandleNotANumberError(void);
int HandleRuntimeError(runtime_error theRuntimeError);
}
#endif
实现文件:ErrorHandlingModule.cpp
//filename:ErrorHandlingModule.cpp
#include <iostream>
#include <exception>
#include <stdexcept>
#include <cstdlib>
#include "ErrorHandlingModule.h"
using namespace std;
namespace SAMSErrorHandling {
void Initialize(void) {
cin.exceptions(cin.failbit);
}
int HandleNotANumberError(void) {
cerr << "Input error - not a number?" << endl;
cin.clear();
char BadInput(5); //Eat the bad input so we can pause the program
cin >> BadInput;
return 1; //An error occurred
}
int HandleRuntimeError(runtime_error theRuntimeError) {
cerr << theRuntimeError.what() << endl;
return 1;
}
}
头文件:PromptModule.h
//filename:PromptModule.h
#ifndef PromptModule_H
#define PromptModule_H
namespace SAMSPrompt {
void PauseForUserAcknowledgement(void);
bool UserWantsToContinueYOrN(const char *theThingWeAreDoing);
}
#endif
实现文件:PormptModule.cpp
//filename:PormptModule.cpp
#include <iostream>
#include "PromptModule.h"
namespace SAMSPrompt {
using namespace std;
void PauseForUserAcknowledgement(void) {
//Note: You must type something before 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"
}
}
主函数:main.cpp
//filename:main.cpp
#include <iostream>
//#include <exception>
#include "ErrorHandlingModule.h"
#include "PromptModule.h"
#include <cstdlib>
using namespace std;
char GetOperator(void) {
char Operator;
cout << "Operator: ";
cin >> Operator;
return Operator;
}
float GetOperand(void) {
float Operand = 1;
cout << "Operand: ";
cin >> Operand;
return Operand;
}
float Accumulate(const char theOperator, const float theOperand) {
static float myAccumulator = 0;
switch (theOperator){
case '+': myAccumulator = myAccumulator + theOperator;
break;
case '-': myAccumulator = myAccumulator - theOperator;
break;
case '*': myAccumulator = myAccumulator * theOperator;
break;
case '/': myAccumulator = myAccumulator / theOperator;
break;
default: throw runtime_error("Error - Invalid operator");
};
return myAccumulator;
}
int main(int argc, char * argv[])
{
SAMSErrorHandling::Initialize();
do {
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;
}
刚开始出现了种种的问题,后来发现是少了头文件#include <stdexcept>,调试了十几天终于成功了,欢喜一下吧