Now go back and read what @cire wrote. Try changing your readRandomWord() routine to
1 2 3 4 5 6
string readRandomWord(vector<string> data)
{
if ( data.size() == 0 ) cout << "This vector is EMPTY\n";
int pos = rand() % data.size(); // get random word from vector
return data[pos];
}
string readRandomWord(vector<string> data)
{
if ( data.size() == 0 )
{
cout << "This vector is EMPTY\n";
return"***EMPTY***";
}
int pos = rand() % data.size(); // get random word from vector
return data[pos];
}
Yes, fair comment: I shouldn't have let it get to the % data.size() line at all in that circumstance. On my system it does at least put out the error message before crashing, so I thought that was sufficient to find the bug ... if lazy on my part, since I didn't allow the program to continue gracefully. I guess that's OK if, like me, you are running from the command line: maybe not so good from an IDE.
Best to go with @Chervil's suggestion, I think.
Maybe check that the input file stream opened as well.