Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,679 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,481 people online right now. Registration is fast and FREE... Join Now!




String subscript out of range

 
Reply to this topicStart new topic

String subscript out of range, arrrr....

Mrafcho001
post 18 Nov, 2005 - 08:02 PM
Post #1


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


On a code i keep getting a "Debug Assertion Failed!" error:

CODE

Program: ...
File: .\xstring
Line 1508

Experession: string subscript  out of range


here is my code

CODE

#include <string>
#include <stdlib.h>
using namespace std;

#ifndef __COLORCODE___
#define __COLORCODE___

char* TagCode(string str)
{
    size_t pos = str.find("int");
    
    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t' || str[pos-1] == '/')
 {
     if(str[pos+3] == char(0x0d) || str[pos+3] == ' ' || str[pos+3] == '\t'
   || pos+3 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+15, "[/color]");
   pos += 15;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("int", pos);
    }

    pos = 0;
    pos = str.find("char");
    
    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+4] == char(0x0d) || str[pos+4] == ' ' || str[pos+4] == '\t'
   || pos+4 == str.size())
     {

   str.insert(pos, "[color=blue]");
   str.insert(pos+16, "[/color]");
   pos += 16;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("char", pos);
    }

    pos = 0;
    pos = str.find("double");
    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+6] == char(0x0d) || str[pos+6] == ' ' || str[pos+6] == '\t'
   || pos+6 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+18, "[/color]");
   pos += 18;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("double", pos);

    }
    pos = 0;
    pos = str.find("bool");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+4] == char(0x0d) || str[pos+4] == ' ' || str[pos+4] == '\t'
   || pos+4 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+16, "[/color]");
   pos +=16;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("bool", pos);
    }
    pos = 0;
    pos = str.find("#include");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+8] == char(0x0d) || str[pos+8] == ' ' || str[pos+8] == '\t'
   || pos+8 == str.size() || str[pos+8] == '<')
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+20, "[/color]");
   pos +=20;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("#include", pos);
    }
    pos = 0;
    pos = str.find("#pragma");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+7] == char(0x0d) || str[pos+7] == ' ' || str[pos+7] == '\t'
   || pos+7 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+19, "[/color]");
   pos +=19;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("#pragma", pos);
    }
    pos = 0;
    pos = str.find("#ifndef");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+7] == char(0x0d) || str[pos+7] == ' ' || str[pos+7] == '\t'
   || pos+7 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+19, "[/color]");
   pos +=19;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("#ifndef", pos);
    }
    pos = 0;
    pos = str.find("#define");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+7] == char(0x0d) || str[pos+7] == ' ' || str[pos+7] == '\t'
   || pos+7 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+19, "[/color]");
   pos +=19;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("#define", pos);
    }
    pos = 0;
    pos = str.find("using");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+5] == char(0x0d) || str[pos+5] == ' ' || str[pos+5] == '\t'
   || pos+5 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+17, "[/color]");
   pos +=17;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("using", pos);
    }
    
    pos = 0;
    pos = str.find("namespace");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+9] == char(0x0d) || str[pos+9] == ' ' || str[pos+9] == '\t'
   || pos+9 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+21, "[/color]");
   pos +=21;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("namespace", pos);
    }
    pos = 0;
    pos = str.find("if");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+2] == char(0x0d) || str[pos+2] == ' ' || str[pos+2] == '\t'
   || pos+2 == str.size() || str[pos+2] == '(')
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+14, "[/color]");
   pos +=14;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("if", pos);
    }
    pos = 0;
    pos = str.find("while");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+5] == char(0x0d) || str[pos+5] == ' ' || str[pos+5] == '\t'
   || pos+5 == str.size() || str[pos+5] == '(')
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+17, "[/color]");
   pos +=17;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("while", pos);
    }
    pos = 0;
    pos = str.find("else");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+4] == char(0x0d) || str[pos+4] == ' ' || str[pos+4] == '\t'
   || pos+4 == str.size() || str[pos+4] == '{')
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+16, "[/color]");
   pos +=16;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("else", pos);
    }
    pos = 0;
    pos = str.find("this");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+4] == char(0x0d) || str[pos+4] == ' ' || str[pos+4] == '\t'
   || pos+4 == str.size() || str[pos+4] == '-')
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+16, "[/color]");
   pos +=16;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("this", pos);
    }
    pos = 0;
    pos = str.find("for");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+3] == char(0x0d) || str[pos+3] == ' ' || str[pos+3] == '\t'
   || pos+3 == str.size() || str[pos+3] == '(')
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+15, "[/color]");
   pos +=15;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("for", pos);
    }
    pos = 0;
    pos = str.find("#endif");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+6] == char(0x0d) || str[pos+6] == ' ' || str[pos+6] == '\t'
   || pos+6 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+18, "[/color]");
   pos +=18;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("#endif", pos);
    }
    pos = 0;
    pos = str.find("return");

    while(pos != string::npos)
    {
 if(str[pos-1] == '\n' || str[pos-1] == ' ' || pos == 0
     || str[pos-1] == '\t')
 {
     if(str[pos+6] == char(0x0d) || str[pos+6] == ' ' || str[pos+6] == '\t'
   || pos+6 == str.size())
     {
   str.insert(pos, "[color=blue]");
   str.insert(pos+18, "[/color]");
   pos +=18;
     }
     else
     {
   pos++;
     }
 }
 else
 {
     pos++;
 }
 pos = str.find("return", pos);
    }
    pos = 0;
    while(pos != string::npos)
    {
 
 pos = str.find('"', pos);
 if(pos == string::npos)
     break;
 str.insert(pos, "[color=red]");
 pos = str.find('"', pos+12);
 if(pos == string::npos)
     break;

 str.insert(pos+1, "[/color]");
 pos += 1;
    }
    pos = 0;

    while (pos != string::npos)
    {
 pos = str.find("//", pos);
 if(pos == string::npos)
     break;
 str.insert(pos, "[color=green]");
 pos = str.find('\n', pos+13);
 if(pos == string::npos)
 {
     str.insert(str.size(), "[/color]");
 }
 else
 {
     str.insert(pos-1, "[/color]");
 }
    }
    
    //dont go below here!
    char* buf;
    buf = (char*)GlobalAlloc(GPTR, str.size());
    strcpy(buf, str.c_str());

    return buf;
}

#endif


Basicaly the function is passed a string and it is suppose to look for the keywords and add before the key word and after it. But if it does infact find the keyword in the string it gives me an error (as shown above).

If the string does not have a keyword in it, it all goes well.

I've done some debugging and i've narrowed the error down to:

CODE

if(str[pos+3] == char(0x0d) || str[pos+3] == ' ' || str[pos+3] == '\t'
   || pos+3 == str.size())


Thats where it seems to get the error, if the "int" is found in the string.




Im compiling this with Visual Studio 8 2005, Code use to work perfectly with Visual Studio .NET 2003.


Thank you.

This post has been edited by Mrafcho001: 18 Nov, 2005 - 08:03 PM
User is offlineProfile CardPM

Go to the top of the page


Mrafcho001
post 19 Nov, 2005 - 07:15 AM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


I've fixed the error.

Its working good now.


The prolem is that when i was checking str[pos-1] == w/e, and pos is zero, it is checking if str[-1] == w/e which i guess triggered a debugging mechanism in the "new" string class or something.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 07:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month