Home > Back-end >  why am I getting the error terminate called after throwing an instance of 'ErrorException'
why am I getting the error terminate called after throwing an instance of 'ErrorException'

Time:03-29

I'm trying to get all the values within a vector but whenever I run this code it just gives me an Error exception I don't know where the error is.

For everyone that is asking vector.h is apart of the standford library

this is the txtfile I'm using

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include "vector.h"  //You can also use #include <vector>

using namespace std;



Vector<string> split(string str, string token) //The saving grace
{
    Vector<string> result;
    while(str.size()){
        int index = str.find(token);
        if(index!=string::npos){
            result.push_back(str.substr(0,index));
            str = str.substr(index token.size());
            if(str.size()==0)result.push_back(str);
        }
        else
        {
            result.push_back(str);
            str = "";
        }
    }
    return result;
}
bool isInteger(string str)
{
    for(int i = 0; i<str.length(); i  )
    {
      if(isdigit(str[i]) == true)
      {
        return true;
      }
    }
    return false;
}
//----------------------------------------------------//
//Lets get the text of the file into vectors
Vector<string> movieTitle(string txtfile)
{
  Vector<string> text; //Contains all of the text
  Vector<string> films; //All the films
  fstream myFile;
  string word;
  myFile.open(txtfile);
  if(!myFile.good())
  {
    cout << "ERROR: FILE NOT FOUND" << endl;
    exit(1);
  }
  while(getline(myFile, word, '\n'))
  {
    //cout << word << endl;
    text  = split(word, "\t");
  }
  for(int i = 0; text.size(); i  )
  {
    if(text[i].find("(") != string::npos && isInteger(text[i]))
    {
      films.push_back(text[i]);
    }
  }
  return films;
}

int main()
{
  Vector<string> test;
  test = movieTitle("movies_mpaa.txt");
  cout << test << endl;
  return 0;
}

Out Put: terminate called after throwing an instance of 'ErrorException' what():
Aborted (core dumped)

I am just confused on where the error is occurring.

CodePudding user response:

This is your error:

   for(int i = 0; text.size(); i  )
   {
    if(text[i].find("(") != string::npos && isInteger(text[i]))

for-loop doesn't work like that, second expression is a boolean expression which is checked against true before entering loop's body. Stanford library class Vector::operator [] acts in same way as std::vector's and throws an exception if i becomes bigger than size()-1. Your loop never stops because size() never would return 0 if it wasn't 0 to begin with, so eventually i becomes greater. Loop should look like

    for(int i = 0; i < text.size(); i  )

If you used backtrace command in GDB after program had stopped, you'd saw the chain of called functions leading to catastrophic stop. You can walk up to your code by using command frame <number>, where <number> would be an item in that list. GDB got extensive online documentation and FAQ, there are god guides how to use it.

  •  Tags:  
  • c
  • Related