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

lines 10-13: not needed. If you want a vector to start with a specific size then just call its resize() method

line 42: str is an empty string, so attempting to access anything beyone str[0] is an error.

Thanks, but after changing:

str to if (vectorOfInformation[iter] == ')')

And inserting vec.resize(5,"");

...it still doesn't work.

I think you need to learn how to use your compiler's debugger so that you don't have to ask us to do your debugging for you. If you used your compiler's debugger you would quickly find that the problem now is at line 52.

Okay, I've changed the erasing part to this:

for (int x = 0; x != vec.size(); x++)
		{
			vec[x].erase(vec[x].length()-1);
			vec[x].erase(0,1);

		}

And it compiles.

But if I write this in main():

cout << getInfo("5135")[0];

It says "Vector out of range."

Not even this would work!:

int main()
{
	string *ptr = &getInfo("5656")[0];
	cout << *ptr;
	return 0;
}

Yes, I tried to use my debugger, and it sets a breakpoint at the line "cout << *ptr;"

I think we're close to a solution! :)

I compiled your program with VC++ 2008 Express and it crashes bigtime. I don't know what compiler you are using but I'd suggest you get a different compiler. The file I used contain just one line that you had in comments at the top of your program -- (2344)(Arne Kristoffer)(Abc 3) It crashes here:

for (int x = 0; x != vec.size(); x++)
		{
			vec[x].erase(vec[x].length()-1);
			vec[x].erase(0,1);

		}

If you can't take the time to use a decent compiler with a great debugger then I don't have the time to do your debugging for you any more.

Well, I used VC++ 2008 Express, and it compiles fine, and it don't crash.

What's strange, is that, if I add "cout << an;" in the start of the function, it prints an. But if I type it inside the for(int iter = 0; iter != vectorOfInformation.size(); iter++), it don't print.

Do you know about any easy to understand guides for VC++ debugging?

EDIT: I've tried to learn some debugging, and it appears that vectorOfInformation don't get filled with any information! It's empty after the while(getline())-loop, which should append all the lines in the textfile to it.

EDIT: I've tried to learn some debugging, and it appears that vectorOfInformation don't get filled with any information! It's empty after the while(getline())-loop, which should append all the lines in the textfile to it.

I use the same compiler, and that's why the program doesn't crash for you. Put a check after the open statement to see if the file was actually opened. I had that problem once and had to change the location of the file.

Well, the MyFile-object has so many details in it, so I can't see if it's opened, but this is the MyFile line in watch:

+ MyFile {_Filebuffer={...} } std::basic_fstream<char,std::char_traits<char> >

Since the program is located in "C:\Users\Arne\Documents\Visual Studio 2008\Projects\Bank_project\Debug", wouldn't "..\\..\\..\\..\\..\\..\\..\\data.dat" be C:\file.dat? (For example?)

I could try locating the file another place.

This is bloody creepy! :)

I moved the file to \\..\\..\\..\\..\\..\\..\\..\\data

It worked. And, you were right, it stops at the erase-loop.

The reason why it stopped, was that i tried to remove "(" and ")" from "", which logically won't happen. So I added

if (!vec[x].empty())

, so that it wouldn't try to delete "(" and ")" where it doesn't exist.

But, by the way, do you know why I have to locate the file somewhere else?

Thank You! :)

>>But, by the way, do you know why I have to locate the file somewhere else?

The file must be in the same folder that your program is running in unless the open statement tells it otherwise. For example ifstream in("c:\\myfolder\\temp.txt");

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