Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

String Subscript out of FUCKING RANGE

Name: Herpy McDerperson 2011-12-12 20:14

Alright, I don't get it....with hand-typed text, or the code from google.com, this works perfectly fine. On other websites, it gives me shit about a string subscript going out of range. The only thing I know for sure is that it occurs in this section of code, towards the end:


//Set vector elements to lines from text file
    //Open txt file
    ifstream myFile;
    myFile.open("parseme.txt");
    while (getline(myFile, line))
    {
        text[linecount] = line;
        linecount++; //count lines to set count for parsing
    }
    //Close txt file
    myFile.close();

//FORMAT TEXT
    for (int i = 0; i < linecount ; i++)
    {
        //Send line from vector to the parse method, set vector element to returned line
        string temp = formatText(text[i]);
        text[i] = temp;
        cout<<text[i]<<endl;
    }


The function:

//Format line of text
string formatText(string line)
{
    string newLine = "";

    const int length = line.length();
       
    //Format line of text to lowercase
    for (int i = 0; i != length; i++)
        line[i] = tolower(line[i]);

    //Omit HTML tags and script tags/content
    for (int i = 0; i != length; i++)
    {
        if (line[i] == '<')
        {
            tag = true;
            newLine += " ";//Add spaces around tags so separate words don't get smushed together

            if ( (line[i+1] == 's' && line[i+2] == 'c' && line[i+3] == 'r') || (line[i+1] == 's' && line[i+2] == 't' && line[i+3] == 'y') ) //Check for script or style tag
                script = true;
        }

        //Copy text that isn't an html tag, script or style
        else if (tag == false && script == false)
            newLine += line[i];

        else if (line[i] == '>')
        {
            tag = false;
            newLine += " ";//Add spaces around tags so separate words don't get smushed together

            if ( (line[i-7] == '/' && line[i-6] == 's' && line[i-5] == 'c' && line[i-4] == 'r') || (line[i-7] == '/' && line[i-6] == 's' && line[i-5] == 't' && line[i-4] == 'y') ) //Check for closing script or style tag
                script = false;
        }
    }

    return newLine;
}


AAAAAAAAAAAAAAAAAAAAAAAAAH

Name: Anonymous 2011-12-12 21:01

aaaah?

Name: Anonymous 2011-12-12 21:14

don't kno shit bout this, but Imma bite
can you guarantee that for
line[i-7] i is atleast 7 etc?

Name: Anonymous 2011-12-12 21:24

So do sepples IDEs not feature debuggers & breakpoints and shit.
You could actually see the string it goes out of range for.

Name: Anonymous 2011-12-12 21:35

>>3
That is a great point! Thanks, I'll make sure I add another condition so that there are at least however many characters on the line

>>4

and yes I'm using Visual Studio 2008 but I don't really know how the breakpoint shit works lol

(On a related note, stop with the SEPPLES shit. It's gay. Like you.)

Name: Anonymous 2011-12-12 21:40

>>5
You know what's really gay, a SEPPLES programmer that doesn't know how to program

Name: Anonymous 2011-12-12 22:10

stop with the SEPPLES shit
SEPPLES, in my /prog/!?

Name: Anonymous 2011-12-13 0:30

>>3

Thanks for the tip, that did it. After at least a few hours of coding I start to overlook the simpler problems, especially when it's a runtime issue and not something I can catch when I try to compile. That shit can be hard to pick out sometimes, especially if it's just 1 character in a jumblefuck of shit

Name: Anonymous 2011-12-13 0:41

>>6

Them's some big words, coming from an anonymous internetard.

Sorry, I must have forgotten that everyone on /prog/ is a natural born coder with absolutely nothing they can ever learn to better their godlike skills! Oh wait....

Name: Anonymous 2011-12-13 3:21

>>9
Just tell him how cool his story is. There are so many cool stories in this board.

Don't change these.
Name: Email:
Entire Thread Thread List