博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编程中的runtime_error问题
阅读量:4509 次
发布时间:2019-06-08

本文共 3327 字,大约阅读时间需要 11 分钟。

      前几天在编程中,代码如下:

头文件: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>,调试了十几天终于成功了,欢喜一下吧

转载于:https://www.cnblogs.com/guochaoxxl/p/6823205.html

你可能感兴趣的文章
文件压缩与解压缩
查看>>
android 搜索自动匹配关键字并且标红
查看>>
Android ViewPager使用详解
查看>>
python爬虫之scrapy的pipeline的使用
查看>>
mysql 1366错误
查看>>
mfc 导出数据保存成excel和txt格式
查看>>
让Android中的webview支持页面中的文件上传
查看>>
UML基础
查看>>
Oracle 从Dump 文件里提取 DDL 语句 方法说明
查看>>
实现winfrom进度条及进度信息提示
查看>>
关于Spring.Net的singleton和singlecall的讨论
查看>>
vue项目目录结构
查看>>
程序员自学路上的一些感悟
查看>>
使用x64dbg分析微信聊天函数并实现发信息
查看>>
robotframework-selenium2library各个版本
查看>>
插入排序
查看>>
LeetCode全文解锁 √
查看>>
[BZOJ 1566] 管道取珠
查看>>
[Codeforces 1060F] Shrinking Tree
查看>>
Winfrom 桌面弹窗拦截 关闭进程简易程序 源代码下载
查看>>