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){

			frags.push_back(line);
			line = "";
		}

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

		frags.clear();

		cases--;
	}

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.

I don't think you need the second while loop.Also post your find_original implementation.

@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.

I would have to agree with caut_baia that we need to see find_original and also some more of your code.

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:

frags.clear();
                cin.clear()
		cases--;
	}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.