0

I'm about to kill myself... This is very elementary stuff, yet I still can't do it.

Let me first give you the code and then explain my problem :

wordListIterator = wordList.begin();
	list<string>::iterator wordListNextIterator = wordList.begin();
	wordListNextIterator++;


	while(wordListIterator != wordList.end())
	{
		/* Counting duplicates */

		int count = 1;

		while(*wordListIterator == *wordListNextIterator)
		{
			count++;

			wordListNextIterator = wordList.erase(wordListNextIterator);

			if(wordListNextIterator != wordList.end())
			{
				wordListNextIterator = wordListIterator;
				wordListNextIterator++;
			}			
		}

		/* Checking whether the word is a palindrome */

		int z = 0;
		int length = (*wordListIterator).length();
		int palindrome = 0;
		char currentWordReverse[100] = "";

		strcpy(currentWord, (*wordListIterator).c_str());
		currentWord[0] += 32;	// Palindrom kontrolü için ilk harfi küçük harfe çeviriyoruz

		for(z = 0; z < length; z++)
		{
			currentWordReverse[z] = currentWord[length - z - 1];
		}

		currentWordReverse[length] = '\0';

		if(*currentWord == *currentWordReverse)
		{
			palindrome = 1;
		}

		currentWord[0] -= 32;	// Kelimenin ilk harfini tekrar büyük harfe çeviriyoruz


		/* Displaying the result*/

		std::cout << currentWord;

		if(palindrome == 1)
		{
			std::cout << " (P)";
		}

		std::cout << " : " << count << "\n";

		wordListIterator++;
		
		if(wordListIterator != wordList.end())
		{
			wordListNextIterator = wordListIterator;
			wordListNextIterator++;
		}
	}

I have a string list with two iterators, the second is the successor of the first.

The problem is caused by the code block shown in red. When I run the program, it processes the list until the last element, after which the program crashes. I know the problem arises from wordListNextIterator overflowing beyond wordList.end() However, no matter how many checks and controls I try, I cannot prevent it from happening and it's driving me crazy.

If I start the main while block as while(wordListNextIterator != wordList.end()) there are no problems, but naturally the last item in the list is not processed.

Any help will be appreciated...

Thanks in advance...

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Narue
0

What problem are you trying to solve with this code? It appears as if you want to take a list of strings, then print out whether or not the each unique string is a palindrome.

>/* Counting duplicates */
This isn't entirely truthful. It looks more like you're removing duplicates and keeping a count of how many removals there were.

>If I start the main while block as while(wordListNextIterator != wordList.end()) there are no problems
Consider the cases where wordListNextIterator is equal to wordList.end() and you shouldn't have much trouble finding your problem. Also keep in mind the restrictions that are placed in wordList.end() (it's not a valid list item, just a sentinel, so you can't do to much with it).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.