I have an assignment that has me reading in a file and creating a token for each string and special characters in it (which isn't the part I need help with). I currently have part of the program working 98% but am getting an error when I execute the program. I've looked around and couldn't solve it the way I need it. Anyway here is the error I'm getting:

"Debug Assertion Failed!

Expression: string subscript out of range "

Now from what I've gathered it has to do with the size of the string & the container size not matching up.

There was an old thread that gave a solution which partially worked but it was skipping the last word of my line (which could be due to the way I currently have the program.

Anyway here is what the line of txt from the file reads: main is awesome

Here is my code at the current moment (which displays all the text as tokens but generates the error

#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::ifstream source;  //create variable for input file

	std::string myToken;   //create string variable to hold data from file
	std::string txtToken;  //variable to house each token as it's created
	bool done = false;  //variable to end do/while once finished
	int k = 0;   //variable to keep program moving through file grabbing valid characters

	source.open("file\\input.txt");  //open file

	if(!source.is_open()) //check to see if file exists if not proceed with statement within {}
	{
		std::cout << "File not found, or cannot be opened!"<<std::endl;  //generate error message for missing file
		return 0;  //end program
	}

	getline(source,myToken);  //read file into memory and store in string variable myToken for seperation

	do
	{

			char s = myToken[k++]; //each character is stored within "s"

			if( (s>='a' && s<='z') || (s>='A' && s<='Z') )  //checks if found character is valid or invalid
			{
				txtToken = txtToken + s; //if character is valid it is stored within txtToken
			}
			else
			{
				std::cout << "Token is: " << txtToken << std::endl; //txtToken is now displayed to screen
				txtToken = ""; //clear txtToken to input next string set
			}

	}while (!done); //finish do/while

	source.close(); //close file

	return 0; //end program
}

I also found that if I remove the myToken[k++] and make it myToken[k] the error goes away but I get no text.

Instead of [k++] why don't you try incrementing k outside of the [] so you know for sure when it is happening. Then output k and compare it to yourString.size() and make sure everything seems kosher. This error definitely means you are accessing the 5th character in a string of length 4, or something equivalent.

Dave

You never set done to True. Your do {} while(!done) is a nice infinite loop. Of course it runs out of the string limits.

david - I tried placing the k++ outside the [] before and it still ran into that error. I ran the debug and step-over to watch what value "s" takes and it goes through like it's suppose to but no mention of the error when I do it that way.

Nez - Hmm good point. I'll load up the project and add that line and see what happens.

I'll post back what I found.

Change your do...while loop to a while loop and use k as your tracker for your exit condition.

while( k != myToken.size())
{
    char s = myToken[k];  // no ++ here
    // rest of your code here
    k++
}

The reason for this is that you are running to infinity with your current loop and as soon as k is larger then myToken.size() you get a failed assert. Having the loop as I presented will ensure that you do not go beyond the range of the string.

Nez & Nathan - Ok I did both suggestions (not together but on their own) along with a few other tweaks and what is strange is while my original error is gone (which is good), it seems to avoid the last word for some reason.

What I got with the error is this output to the screen:

Token is: main
Token is: is
Token is: awesome

Now doing it either of your suggestions the error is gone but now I get this output:

Token is: main
Token is: is

it skips the last word for whatever reason.

Here is what I currently have

getline(source,myToken);
	while(k != myToken.size())
	{
	//getline(source,myToken);  //read file into memory and store in string variable myToken for seperation


			char s = myToken[k]; //each character is stored within "s"
			k++;

			if( (s>='a' && s<='z') || (s>='A' && s<='Z') )  //checks if found character is valid or invalid
			{
				txtToken = txtToken + s; //if character is valid it is stored within txtToken
			}
			else
			{
				std::cout << "Token is: " << txtToken << std::endl; //txtToken is now displayed to screen
				txtToken = ""; //clear txtToken to input next string set
			}

	}
	source.close(); //close file

	return 0; //end program
}

Now if I put k++ prior to the char s = myToken[k], I get everything from "a" on meaning:

Token is: ain
Token is: is
Token is: awesome

I know the reason it skips m is because it jumps to the next spot before it even puts the value in txtToken. I've placed k++ at the end of the while loop but it gives me the error I reported last.

Now another weird thing is as it is currently if I change what the contents are of my input.txt I get no display. For example if I put "main isawesome" (excluding the "") all I get is main nothing else.

Also if I change the line all together to anything other than main I get nothing displayed.

This article has been dead for over six months. Start a new discussion instead.