编程中的runtime_error问题
来源:互联网 发布:ubuntu切换到登陆界面 编辑:程序博客网 时间:2024/05/21 04:23
前几天在编程中,代码如下:
头文件: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>,调试了十几天终于成功了,欢喜一下吧
- 编程中的runtime_error问题
- 关于throw runtime_error("IO stream corrupted");的一个问题
- 串口编程中的问题
- 编程中的对齐问题
- 编程中的小问题
- WINCE编程中的问题
- c编程中的问题
- 编程中的 默认问题
- 编程中的数学问题
- 编程中的原则问题
- 编程中的隐喻问题
- std::runtime_error 异常传递失败
- 多线程编程中的本质问题
- VC8.0中的编程问题
- Java编程中的编码问题
- 解决编程中的乱码问题
- QT编程中的小问题
- Java编程中的编码问题
- PHP获取当前url的具体方法
- PHP获取当前url路径的函数以及服务器变量
- php入门教程 第一个php程序
- rsync与inotify 数据同步
- Python动态类型理解
- 编程中的runtime_error问题
- 股市风云
- linux 新建用户、用户组 以及为新用户分配权限
- Codeforces Round #215 (diy.2) B.Sereja and Suffixes
- Makefile条件编译debug版和release版
- HTML5开发中使用MVC模式
- 深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing) 第四节 参数传递对堆栈的影响 2
- makefile及条件编译
- hdu 4213 Sokoban(模拟)