I am trying to run what i thought was a pretty basic program to solve a problem concerning strings (besides the point). It turns out that my algorithm works properly but i am stuck on a detail; the following segment of code is the problem:

int cases;
	string line;
	vector<string> frags, results;

	cin >> cases;

	while ( cases > 0 ) {
		while (cin >> line){

			line = "";

		results.push_back( find_original(frags) ); 
                          //find_original does not modify frags, and returns a string 



the problem occurs when i input a value of 'cases' that is higher than 1. the error occurs during the second run-through when the program hits the inner while loop. I get a debugger error telling me that a vector iterator is not dereferenceable. Any help is greatly appreciated.

5 Years
Discussion Span
Last Post by Coffee_Table

@caut baia - the inner while loop is so that i can read in an arbitrary amount of strings (which is essential to the problem) until i terminate with an EOF character (^Z). posting find_original will not help because i am not passing frags by reference nor am i modifying it at all, other than with frags.clear() . Further, i ran a number of watches while debugging and there didn't seem to be any funny business. The key point is that the program went fine if i let cases = 1. Even when i let cases >= 1, the first run-thru of the loop worked. only when i got to the second run-thru did i get this vector iterator error.


problem solved! turns out that "not deferenceable vector iterator" error was a result of a more primary problem with the program; Everything works smoothly until the second run-thru...why? well, here's what was happening: on the first run-thru, the progran would clear the contents of frags, leaving it empty. on the second run-thru, the inner while loop would be skipped because the input stream's last input was the EOF marker! then the program would try to pass the empty frags to find_original, creating all sorts of problems...thus, all i needed to do was add the single line:


this flushed the input stream and allowed the program to function normally.

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.