抛出"int"实例后调用的终止
terminate called after throwing an instance of 'int'
当我尝试输入糟糕的名称(非数字或字母数字)时,我想抛出 exption。 第一次它运行良好,但是当我从 catch 调用再次进入功能并再次输入错误名称时,我收到此错误:在抛出'int'实例后终止调用"
int main(int argc, char *argv[]) {
int numberOfWarrior = atoi(argv[1]);
int numberOfthief = atoi(argv[2]);
int numberOfnecromancer = atoi(argv[3]);
int vecorSize = numberOfnecromancer + numberOfthief + numberOfWarrior;
vector<Hero*> turnOfPlayer;
//Enter Warrion Players
if(numberOfWarrior>0) {
try {
enterWarrior(0, turnOfPlayer, numberOfWarrior,"warrior");
}
catch (int i) {
cout << "Invalid name of user. You can only choose letters or numbers. Try again please." << endl;
enterWarrior(i, turnOfPlayer, numberOfWarrior, "warrior"); // my program terminate when i enter to function from here
}
}
void enterWarrior(int index, vector<Hero*> v,int numOfWarrior, std::string Type)
{
std::string nameOfwarrior;
for(int i=index; i<numOfWarrior; i++)
{
cout << "Please insert " << Type << " number " << i+1 << " name:";
cin >> nameOfwarrior;
if(!digitCheck(nameOfwarrior))
throw i; // in the second time i get the error here
if(Type.compare("warrior")==0) {
Warrior *warr = new Warrior(nameOfwarrior);
v.push_back(warr);
}
if(Type.compare("thief")==0) {
Thief *thief = new Thief(nameOfwarrior);
v.push_back(thief);
}
if(Type.compare("necromancer")==0) {
Necromancer *nec = new Necromancer(nameOfwarrior);
v.push_back(nec);
}
}
}
我不知道如何解决它谢谢
正如在
评论中所说的那样,只需使用循环,例如:
if(numberOfWarrior>0) {
int firstIndex = 0.
for (;;) {
try {
enterWarrior(firstIndex, turnOfPlayer, numberOfWarrior,"warrior");
// if we are here that means all is ok, go out of the loop
break;
}
catch (int i) {
cout << "Invalid name of user. You can only choose letters or numbers. Try again please." << endl;
firstIndex = i;
}
}
}
警告 void enterWarrior(int index, vector<Hero*> v,int numOfWarrior, std::string Type)
向量由值给出,因此 enterWarrior 修改了向量的副本,因此通过 void enterWarrior(int index, vector & v,int numOfWarrior, std::string Type)' 更改它。
Type.compare("warrior")==0
等不是很容易阅读,你可以用(Type == "warrior")
等替换它们,我们是用 C++ 而不是 Java
一些"如果"可以是"else if",当前面的"如果"为真时,不会一无所获。如果类型不是预期的,则似乎也缺少最后一个其他内容,以指示问题
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- 在抛出 'std::invalid_argument' 的实例后终止调用 what(): stoi (找不到问题所在)
- 终止调用本机代码的 .Net 线程
- C++程序问题:抛出'std::invalid_argument'实例后终止调用
- 当可以返回错误/异常时,从库中终止调用程序(例如,调用exit())是否总是错误的?
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 在抛出 'std::bad_weak_ptr' 的实例后终止调用 what(): bad_weak_ptr?
- 调试"在抛出 ..) 实例后终止调用",当异常 _should_ 被捕获时
- 引发实例后终止调用 - C++错误
- 在抛出 std::bad_alloc 实例后终止调用.使用两个类,一个带有指向另一个的指针
- 在抛出"pqxx::syntax_error"的实例后终止调用?
- C++11 快速排序终止调用,没有活动异常
- C++:在没有活动异常(GCC)的情况下终止调用
- 在抛出 std::string 实例后终止调用
- 在抛出"std::bad_alloc"实例后终止调用
- 在抛出"std::out_of_range"实例后终止调用