如何修复抛出std::out_of_range实例后调用的terminate
How to fix terminate called after throwing an instance of std::out_of_range
留言如下:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase: __pos (which is 18446744073709551615) > this->size() (which is 22)
Aborted (core dumped)
下面是我到目前为止的代码(它应该在一个混乱的数据文件中重新格式化名称,日期和金额,使其看起来像:
Foster, Jerry Lee 1995 329,475
//This program reformats the retirement account data file oldretirement.txt and outputs the data into a new file newretirement.txt
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cctype>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
void getridof(string &line);
int splitamount(string &line);
int splityear(string &line);
string splitfullname(string &line);
int main(){
ifstream fin;
ofstream fout;
string line;
string finalname;
size_t found;
int finalamount;
int finalyear;
fin.open("oldretirement.txt");
if(fin.fail())
{cout<< "input file failed to open. n";
exit (1);}
fout.open("newretirement.txt");
if(fout.fail())
{cout<<"output file failed to open.n";
exit (1);}
while(!fin.eof()){
getline(fin,line);
// getridof(line);
finalamount = splitamount(line);
finalyear = splityear(line);
getridof(line);
// found = line.find(",");
// if(found!=string::npos)
// {finalname = line;}
// else
// {finalname = splitfullname(line);}
fout << finalname << finalyear << finalamount << endl;
}
fin.close();
fout.close();
}
void getridof(string &line){
int location = 0;
location=line.find("Account$:");
cout << location << endl;
if(location != 0){
// location=line.find("Account$:");
line.erase(location,9);}
cout<< "get rid of a " << line << endl;
location = line.find("born:");
if(location != -1){
// location=line.find("Account$:");
line.erase(location,5);}
cout<< "get rid of b " << line << endl;
return;
}
int splitamount(string &line){
int a;
size_t found = -1;
int num_len;
line.erase(line.find_last_of(","),1);
cout << "amount a " << line << endl;
found=line.find_last_of((" "));
num_len=line.length()-found;
char amount[num_len];
for(int i=0;i<num_len;i++){
amount[i] = line[found + 1 + i];
}
line=line.substr(0,found);
cout << "amount b " << line<< endl;
a=atoi(amount);
cout << "amount c " << a << endl;
return a;
}
int splityear(string &line){
char year[4];
int y;
size_t found = -1;
cout << "split year a " << line << endl;
found = line.find_first_of("1",1);
for(int i=0;i<4;i++){
year[i] = line[found+i];
}
line=line.erase(found,4);
y=atoi(year);
cout << "split year b " << line << endl;
cout << "split year c " << y << endl;
return y;
}
string splitfullname(string &line){
string last;
string rest = ", ";
string fullname;
size_t found = -1;
found = line.find_last_of(" ");
last = line.substr(found+1);
// line.erase(found,string::npos);
rest.insert(3,line);
fullname = last + rest;
return fullname;
}
//temp=line.substr(line.first_of("1",0))
//get today's date and subtract integer version of birth dates in data from today's date. (finalyear atoi) (age = currentyear-intyear) do in main.
变化:
if(location != 0){
if(location != string::npos){
如果没有找到匹配的std::string::find()
,该函数返回string::npos
。npos
是一个静态成员常量值,具有size_t类型元素的最大可能值。
这就是您看到18446744073709551615
值的原因,
std::out_of_range ' .
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 从基类实例调用派生类方法而不进行强制转换
- 如何在C++从父实例调用子函数
- 为 std::shared_ptr 实例调用 Operator()
- 跨类的多个实例调用函数一次
- 如何从派生类实例调用父类重载函数
- 如何在C++中跨实例调用方法
- 防止同一类的实例调用公共功能(C )
- 使用Objective C类实例调用C函数
- 从结构实例调用变量语法问题C++
- 从基类实例调用派生类方法
- C++从基类实例调用派生函数
- 是否可以从不存在的实例调用方法?
- 如何使用派生类实例调用基类重载的func,
- 为singleton实例调用函数创建一个别名
- 用结构体的所有实例调用函数
- 从继承的类实例调用静态成员
- 当我从类实例调用方法时,我的C程序崩溃了,下面是代码
- 当尝试使用类的实例调用方法时,调试断言错误
- C++:从派生实例调用 base 中的纯虚拟方法重载