Question

Given a global vector list of ASCII codes and corresponding number values and a string, like 000.00-000.0.0.0, this function takes an input token strings 2-char or 3-char long and replaces it with a single ASCII symbol that represents the number value between 0 and 184, then returns the shortened string without deliminator as out. Also, in reverse (direction 1) given the ASCII symbol it converts back to the number string and returns.

//looks for input string in vector and returns output, 'c' is check row, 'r' is return row
string vectorSearch(string &check, int n, int c, int r) 
{
    if (check.length() <= 1) 
        return check;
    if (list[n][c] == check || list[n][c] == ('0'+check)) //adds leading zero if 2char long
        return list[n][r];
    else 
        return vectorSearch (check, ++n, c, r);
}

//this function takes an ontology and either changes from single char 
//to string or takes strings and converts to char representation
string Lexicon::convertOntology(string input, int direction, string out, string temp) 
{
    if (input == "" && temp == "") 
        return out; //check for completed conversion
    else {
        if (input[0] == '.' || input[0] == '-' || input == "") { //found deliniator or endk
            if (input != "") return convertOntology(input.substr(1),direction, 
                 out+=vectorSearch(temp, 0, direction, 1-direction), "");
            else return convertOntology("", direction, 
                 out+=vectorSearch(temp, 0, direction, 1-direction), "");
        } else 
            return convertOntology(input.substr(1), direction, out, temp+=input[0]); //increment and check
    }
}

These functions work fine except for on output after the last char is parsed. With a break on the line return convertOntology(input.substr(1), direction, out+=add, temp); there is an error when input == "" and temp == "0" - the last pass through vectorSearch() should clear the temp and add the temp char to the out string, since the temp is == 1char then it should be returned from vectorSearch() as it is. Then clear the convertOntology() return check of input and temp == "". But, it never reaches a break on the first line of vectorSearch() and there is an

Unhandled exception at 0x77bc15de exception: std::out_of_range at memory location 0x0035cf1c

What is going on? Is this an issue with recursion backtracking through returns and I am missing a return somewhere to break the recursion loop?

Was it helpful?

Solution

for the case where temp == "" and input != "" you call input.substr(1) which is, well, out of range.

OTHER TIPS

Even if you don't get to the else part,

input.substr(1)

will throw an exception when the input string is exactly one character long.

Seems it doesn't - input.substr(input.size()) is allowed, and returns an empty string.


You will later likely have a similar problem in VectorSearch. If there is no match, you will increment n until it gets out of range.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top