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.