I am trying to copy in strings from a .txt
file (every word is on a new line) into an array. The main routine would look like this.
const int MAXDICTWORDS = 5000;
int main()
{
string dict[MAXDICTWORDS];
ifstream dictfile; // file containing the list of words
dictfile.open("words.txt");
if (!dictfile) {
cout << "File not found!" << endl;
return (1);
}
int nwords = dicReader(dictfile, dict);
// dict should now hold array of words from words.txt
// nwords will be the amount of words in the array
}
This is my current implementation of dicReader
. dict
will always be empty when passed to this function. I am practicing with recursion, so no while
or for
loops can be used. Any ideas on what I am missing?
int dicReader(istream &dictfile, string dict[])
{
int count = 0; // know this is wrong, recursive call will always reset count to 0
string item;
if (!dictfile.eof())
{
dictfile >> item;
dict[0] = item;
dicReader(dictfile, dict + 1); // is this the correct recursive call?
count++;
}
return count;
}