substring position variable causing crash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
while(!flive.eof())
	{
		getline(flive,slive);
		pos = slive.find("	");
		reclive = slive.substr(pos);
		slive.erase(pos);
		keylive = slive;
		backups.insertItem(slive);
		found = false;

		while(!fbackup.eof())
		{
			getline(fbackup,sback);
			pos = sback.find("	");
			recback = sback.substr(pos);
			keyback = sback;
			keyback.erase(pos);

			if(keylive.compare(keyback) == 0)
			{
				found = true;

				if(reclive.compare(recback) != 0)
				{
					changed.insertItem(keylive);
					found = false;
				}
			}
		}
		fbackup.clear();
		if(found == false)
		{
			added.insertItem(keylive);
		}
	}


I am writing a string manipulation program, but when I try to use pos to keep track of the location of "tab" in the string that was read in from a file it crashes the whole program when I try to run it. When I step through the program it works for the first time it runs through the nested while loop until the end of the file [while(!fbackup.eof())], but when I get to the second time it runs that while loop it causes an "Unhandled exception at 0x7c812afb" and says "std::out_of_range at location..." I am just following the format that the string examples on this website give. I need this for my final project, any help would be wonderful. Thank you in advance.
If .find() doesn't find the value, it returns string::npos, which is not a valid position for your .erase().
But I know for a fact that there is a there is a tab in it. maybe I just need to look at it again, thanks.
Whenever you make an assumption about a condition, add an assert to make sure the condition is actually true.
asserts are a great help finding bugs early.
Last edited on
Saying that you're certain something is somewhere is asking for all hell to break loose.

-Albatross
Last edited on
I tried using assert, but I'm still getting the same error.
Somehow I get the feeling you should be using "\t" as your search parameter in place of an actual tab. Lemme get some coffee...

-Albatross
I used "\t" and I still get the same error.
In that case... WAIT. Wait... silly me... are you checking to make sure that the position is valid?

http://cplusplus.com/reference/string/string/find/
http://cplusplus.com/reference/string/string/npos/

-Albatross
I can't think of a reason for the position to not be valid. Not only do I know that the position is valid because I wrote the test files, but in earlier testing where I had a couple of errors that made it insert everything to only one of the binary search trees I wasn't getting this error.
Well, if the position isn't valid, then there are no \t in your strings. Use a debugger and make sure that the data you are reading is actually what you think it is.
Curious.

Be on the safe side and put a form of checking if there are tabs in your file.

-Albatross
Last edited on
That's actually part of the assignment, but for now I just wanted to make sure that it worked when there were tabs. But now I'm out of time and I have to go and turn it in incomplete, thanks for the help anyway.
Topic archived. No new replies allowed.