Reputation: 23
I am getting a List Iterator Not incrementable on the code below after adding a stringstream for movieName and am unsure as how to go about solving the error. I am trying to read in from a file and add the items into a list then itterate through the list and add eash word from the title of the movie into another list. Any help would be appreciated!
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
list <string> movieList;
list <string>::iterator iterMovie;
list <string> *titleWordList = new list <string>;
list <string>::iterator iterTitleWord;
ifstream inFile;
inFile.open("JamesBond.txt");
if (!inFile)
{
cout << "Could not find the specified file.";
}
else
{
movieList.clear();
string movieName;
while(getline(inFile,movieName))
{
movieList.push_back((movieName));
/*
for each(string movieName in movieList)
{
titleWordList->push_back(movieName);
}
*/
stringstream ss(movieName);
while (ss >> movieName)
{
titleWordList->push_back(movieName);
}
}
}
if (movieList.empty())
{
cout << "No Data Found!" << endl;
}
else
{
cout << "Writing Output: \n\n";
for (iterMovie=movieList.begin(); iterMovie !=movieList.end(); ++iterMovie)
{
cout << *iterMovie << endl;
}
for (iterTitleWord=movieList.begin(); iterTitleWord != movieList.end(); ++iterMovie)
{
cout << *iterTitleWord << endl;
cout << &titleWordList;
}
}
}
Upvotes: 0
Views: 849
Reputation: 73443
In the second for
there is a copy paste error, it should be ++iterTitleWord
instead of ++iterMovie
.
Also, as @Greg suggested, iterTitleWord=movieList.begin()
and iterTitleWord != movieList.end()
should be iterTitleWord=titleWordList->begin()
and iterTitleWord != titleWordList->end()
respectively.
Upvotes: 2