Hi!
I have this function (well, it's pretty good explained in the code-tag, but okay.) which is supposed to see if a string is found first in a line in a text file. Anyway, this code gives the message (while running in debug mode in MSVC++):
"Debug assertion failed. Expression: Vector subscript out of range"
This is my function. I'm sorry, I know it's a long code, but I don't know whats wrong, so I wouldn't cut to much off it.
I have also added comments, so it would be easyer to read.
Code:
//getInfo function. Returns an empty vec if an (account name) is not found. If an is found, the function will return the rest of the contents of the line starting with an in the text file.
//An example of a line in the text file could be: "(2344)(Arne Kristoffer)(Abc 3)". In this example, if we called getInfo("2344"), it would return a vector consisting of three elements,
//"2344", "Arne Kristoffer" and "Abc 3"
vector<string> getInfo(const string &an)
{
string str;
ifstream MyFile("data.txt");
vector<string> vec;
vec.push_back("");
vec.push_back("");
vec.push_back("");
vec.push_back("");
int position = 0;
vector<string> vectorOfInformation;
string line;
// Reads all info from file into an vector
while(getline(MyFile, line))
vectorOfInformation.push_back(line);
//loops through the "file"
for(int iter = 0; iter != vectorOfInformation.size(); iter++)
{
//Clears the temporary storage vector for new contents.
vec.clear();
vec.push_back("");
vec.push_back("");
vec.push_back("");
vec.push_back("");
vec.push_back("");
//Loops through vectorOfInformation[i], and store the contents of the string (or is it a substring, but anyway...) in the vector<string> vec.
for (size_t i=0; i < vectorOfInformation[iter].length(); i++)
{
if (vectorOfInformation[iter][i] != '(' || vectorOfInformation[iter][i] != ')')
{
vec[position] +=vectorOfInformation[iter][i];
if (str[i] == ')')
position++;
}
}
// We now have an vector (vec) consisting of an unknown number of elements. First, we have to split away the "(" and ")":
int iteriter = 0;
for (int x = 0; x != vec.size(); x++)
{
vec[iteriter].erase(vec[0].length()-1);
vec[iteriter].erase(0,1);
iteriter++;
}
//We now have the clean vec, with no "(" and ")", and have to decide wether to return vec (which means that vec[0] == an) or not.
if (vec[0] == an)
{
return vec;
}
}
//We now have looped through the entire text file, but found nothing. We have to return an empty vector.
vector<string> empty;
return empty;
}
I'm new to this language, so please, don't make a new code for me, just point out the things ive done wrong, and I will correct it.
:)
By the way, here is a link to pastebin:
http://pastebin.com/m36979d03
Arne Kristoffer