0

I am running into an error during my program's runtime:
"Debug Assertion Failed! ... Expression: string subscript out of range."

This is happening in my for loop when my if statement attempts to check if the character at 'i' in the string isDelimiter() or isOperator(). I am passing the char 'check' as an attribute, and in the comments I have made sure that 'check' is grabbing the correct character. I've been working on this issue for a while and I can't seem to resolve it.

EDITED AT BOTTOM

    string inputLine = "";
    string inputString = "";

    int main()
    {
        ifstream input("input.txt");
        getline(input, inputLine);

        if (input.is_open())
        {
            while (!input.eof())
            {
                getline(input, inputLine);

                for (int i = 0; i<inputLine.length(); i++)
                {
                    char check = inputLine[i];
                    //cout << check << "\n"; // test for correct character

                    if ((inputLine[i] != ' ') || (inputLine[i] != isDelimiter(check)) || (inputLine[i] != isOperator(check)))
                    {
                        inputString = inputString + inputLine[i];
                        //cout << lexer(inputString) << "\n";
                        //cout << inputString;
                    } // end if
                    else
                    {
                        cout << lexer(inputString);
                        if (inputLine[i] == isDelimiter(i))
                    cout << inputLine[i] + "\tDELIMITER";
                        if (inputLine[i] == isOperator(i))
                            cout << inputLine[i] + "\tOPERATOR";
                        inputString = "";
                    } // end else
                    //cout << inputString << "\n";
                } // end for
            } // end while
            //input.close();
        }
        else cout << "Unable to open file.";

        return 0;
    }

Here are the isDelimiter() and isOperator() methods.

    bool isOperator(char c)
    {
        if ((inputLine[c] == '+') || (inputLine[c] == '-') || (inputLine[c] == '*') || (inputLine[c] == '/') || (inputLine[c] == '=') || (inputLine[c] == '%') || (inputLine[c] == '<') || (inputLine[c] == '>'))
            return true;
        else
            return false;
    }

    bool isDelimiter(char c)
    {
        if ((inputLine[c] == ';') || (inputLine[c] == '(') || (inputLine[c] == ')') || (inputLine[c] == ','))
            return true;
        else
            return false;
    }

Any help is appreciated!

EDIT::

After reviewing my code some more I realized the mistake, but I still have another. That runtime error was because in my isOperator() and isDelimiter() functions, I was checking inputString[c] rather than just 'c'. Silly mistake, I know. However, although there is no longer an error, the program still skips checking the isOperator() and isDelimiter() methods, and only goes into the else statement when it reads a ' '. Why isn't it going into my else statement for operators and delimiters?

Dreiak
  • 69
  • 3
  • 7

2 Answers2

1

Your functions take char, change them to int:

bool isDelimiter(char c) //should be int
{
    if ((inputLine[c] == ';') || (inputLine[c] == '(') || (inputLine[c] == ')') || (inputLine[c] == ','))
        return true;
    else
        return false;
}
yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • After changing the functions and the attributes being passed to ints, I do not have the error, but my code still skips the else statement which should be entered as soon as the first ';' is read. And initially I was also passing the attributes into the functions as characters, because they are what needs to be checked in the functions ('+', '%', etc.), am I correct? How would it do that with int? – Dreiak Feb 06 '14 at 02:33
0

I figured it out, and even though they're simple mistakes, maybe it'll help somebody in the future. After all, I was stumped for a while.

They were mainly just syntax errors, I really needed to freshen up on my coding.

This is the format my functions now follow. Notice I'm finally passing the correct values.

    bool isDelimiter(char c)
    {
        if ((c == ';') || (c == '(') || (c == ')') || (c == ','))
            return true;
        else
            return false;
    }

And this is how I am now calling them. I was trying to check if the input itself was equal to the function, which would be like asking if "helloworld" == TRUE. Obviously that wasn't working, and I wanted to kick myself once I figured that out. Here's the snippet:

            if ((inputLine[i] == ' ') || (isDelimiter(check)) || (isOperator(check)))
            {
                cout << lexer(inputString);
                if (isDelimiter(check))
                {
                    cout << inputLine[i] << "\t\tDELIMITER\n";
                }
                if (isOperator(check))
                {
                    cout << inputLine[i] << "\t\tOPERATOR\n";
                }
                inputString = "";

            } // end if
            else
            {
                inputString = inputString + inputLine[i];
            } // end else

That has resolved my basic issues, now onto bigger ones.

Dreiak
  • 69
  • 3
  • 7