c++ - How to fix terminate called after throwing an instance of std::out_of_range -
here message:
terminate called after throwing instance of 'std::out_of_range' what(): basic_string::erase: __pos (which 18446744073709551615) > this->size() (which 22) aborted (core dumped)
and below code far (it's supposed reformat names , dates , amounts in scrambled data file looks like:
foster, jerry lee 1995 329,475
//this program reformats retirement account data file oldretirement.txt , outputs data 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 open. \n"; exit (1);} fout.open("newretirement.txt"); if(fout.fail()) {cout<<"output file failed 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 " << 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 " << 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 " << << endl; return a; } int splityear(string &line){ char year[4]; int y; size_t found = -1; cout << "split year " << 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 , subtract integer version of birth dates in data today's date. (finalyear atoi) (age = currentyear-intyear) in main.
change :
if(location != 0){
to
if(location != string::npos){
if no matches found std::string::find()
, function returns string::npos
. npos
static member constant value greatest possible value element of type size_t.
that reason seeing value 18446744073709551615
, cause of
std::out_of_range`.
Comments
Post a Comment