I'm trying to convert an old program I wrote in C to C++. One of the parts requires reading in a dictionary file, and putting each word into a vector that only contains words of that length. All the smaller vectors will be put in a bigger, outer vector. In this basic example, I'm trying to just read in a few words and print out words of a given size. When I try to test it, no words are printed out even though words of that size exist in the file. What is wrong with this code?
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
using namespace std;
vector< vector<string> >readDictionary(void)
{
vector< vector<string> > outer;
vector<string>::iterator iterator;
int letters;
ifstream dict;
for(int i = 0; i < 29; i++)
{
vector<string> inner;
outer.push_back(inner);
}
dict.open("dictionary.txt");
if(!dict.is_open())
{
cout << "Error opening the dictionary. Exiting" << endl;
exit(1);
}
while(!dict.eof())
{
string word;
getline(dict,word);
if(word.size() > 0)
{
vector<string> inner = outer.at(word.size() - 1);
inner.push_back(word);
}
}
cout << "Letters: ";
cin >> letters;
vector<string> inner = outer.at(letters - 1);
for(iterator = inner.begin(); iterator != inner.end(); iterator++)
{
cout << *iterator << endl;
}
return outer;
}