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

头文件: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问题的更多相关文章

  1. 你不知道的this—JS异步编程中的this

    Javascript小学生都知道了javascript中的函数调用时会 隐性的接收两个附加的参数:this和arguments.参数this在javascript编程中占据中非常重要的地位,它的值取决 ...

  2. Java EE 编程中路径

    版权声明:未经博主允许,不得转载 首先我们要限定一个范围,是一个项目,或是以个访问地址..就先以一个项目为限定的范围 前述: 学过物理学的都知道相对运动和绝对运动, 虽然是相似的概念,但这里的要简单得 ...

  3. 并发编程中.net与java的一些对比

    Java在并发编程中进行使用java.util.concurrent.atomic来处理一些轻量级变量 如AtomicInteger AtomicBoolean等 .Net中则使用Interlocke ...

  4. Java编程中“为了性能”尽量要做到的一些地方

    最近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于随意化,这些不好的习惯或对程序语言的不了解是应该好好打压打压了. 下面是参考网络资源总结的一些在Ja ...

  5. 第51讲:Scala中链式调用风格的实现代码实战及其在Spark编程中的广泛运用

    今天学习了下scala中的链式调用风格的实现,在spark编程中,我们经常会看到如下一段代码: sc.textFile("hdfs://......").flatMap(_.spl ...

  6. Windows编程中UNICODE和_UNICODE定义问题

    Windows编程中UNICODE和_UNICODE定义问题 先转一篇文章: 初学Windows SDK编程时碰到过这个问题,相信很多初学Windows编程的人也都碰到过,后来慢慢搞明白些了,但有时别 ...

  7. UDP编程中client和server中使用recvfrom和sendto的区别

    client中:      sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&saddr,len);      recvfrom(sfd,buf ...

  8. Attribute在.net编程中的应用

    Attribute FYI Link: Attribute在.net编程中的应用(一) Attribute在.net编程中的应用(二) Attribute在.net编程中的应用(三) Attribut ...

  9. TCP/IP网络编程中socket的行为

    一. read/write的语义:为什么会阻塞? 先从write说起: #include <unistd.h>ssize_t write(int fd, const void *buf, ...

随机推荐

  1. python基础之内置函数补充、匿名函数、递归函数

    内置函数补充 python divmod()函数:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b) 语法: 1 divmod(a, b) #a.b为数字,a为除数 ...

  2. SSM框架的简单搭建

    转:https://blog.csdn.net/zhshulin/article/details/37956105 Spring+SpringMVC+MyBatis spring       : 4. ...

  3. CodeForces 771C Bear and Tree Jumps 树形DP

    题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...

  4. Centos7 查看Mysql配置文件

    my.cnf是mysql启动时加载的配置文件,一般会放在mysql的安装目录中,用户也可以放在其他目录加载. 安装mysql后,系统中会有多个my.cnf文件,有些是用于测试的. 使用locate m ...

  5. svn Previous operation has not finished; run 'cleanup' if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...

  6. 为 DirectAccess 设计 DNS 基础结构

    TechNet 库Windows ServerWindows Server 2008 R2 und Windows Server 2008浏览 Windows Server 技术NetworkingD ...

  7. 为什么工具类App,都要做一个社区?

    非著名程序员涩郎 非著名程序员,字耿左直右,号涩郎,爱搞机,爱编程,是爬行在移动互联网中的一名码匠!个人微信号:loonggg,微博:涩郎,专注于移动互联网的开发和研究,本号致力于分享IT技术和程序猿 ...

  8. USACO刷题之路,开始了

    几天前,重新开始刷题了. 重新刷题有几个原因: 1.曾经的OI经历,如今除了悟性高些.知识多些,大多已经遗忘.不希望真的让之前的OI水平就这么丢了. 2.越来越觉得,刷题真的是一件很开心的事情.大学中 ...

  9. java初学1

    1.Java主要技术和分支以及应用领域 (1)Java SE Java Platform,Standard Edition,Java SE 以前称为J2SE.它允许开发和部署在桌面.服务器.嵌入式环境 ...

  10. 用js做的表单的增,删,以及全选

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>创 ...