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.

Dani AI

Generated

Short expert note: the assertion about a "not dereferenceable" vector iterator was a symptom rather than the root cause. The inner extraction loop exhausted the input stream and left it in an error/EOF state; on the second outer iteration the extraction loop immediately failed, leaving the fragments container empty and causing iterator dereferences inside find_original to blow up. 's follow-up confirms that resetting the stream state removed the symptom. and were right to ask for more code — an empty container or a stale cin is a very common hidden trigger.

Safer input patterns that prevent this class of bugs: avoid using global EOF as a per-case delimiter. When the number of cases is known, drive parsing by that count. When each case has an unknown number of tokens, prefer an explicit delimiter (blank line or marker) and parse line-by-line with std::getline plus std::istringstream. That localizes parsing to a case and prevents exhausting the whole std::cin.

Example pattern (parse cases separated by a blank line):

#include <sstream>
#include <string>
#include <vector>

for (int t = 0; t < cases; ++t) {
    std::vector<std::string> frags;
    std::string line;
    while (std::getline(std::cin, line) && !line.empty()) {
        std::istringstream iss(line);
        std::string token;
        while (iss >> token) frags.push_back(token);
    }
    if (frags.empty()) {
        // handle empty-case (skip, default, or an error)
    } else {
        results.push_back(find_original(frags));
    }
}

Practical checks and troubleshooting tips: add a guard (if (frags.empty())) before dereferencing; log frags.size() before calling processing functions; use assertions during development; parse with a redirected input file to reproduce reliably. If a stream is in EOF/fail state but more input is expected, reset the state and discard leftover input before continuing; however, prefer eliminating the need to do that by using explicit per-case delimiters.

Recommended Answers

All 4 Replies

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.