I've been trying to fix this for ages,
I've got a program that takes in a random word from a supplied dictionary (txt file), and then adds this word to a vector of strings.
The randomWord() function works as intended, but the getWords() seems to mash-up everything when I try to print out the vector.
The vector declaration:
vector<string> pass_words; //Array of words used in password
These are the two functions:
//gets a random word from the wordlist file.
string randomWord()
{
wordfile.open ("wordlist.txt"); //open the wordlist file
string wordonline;
string word;
int wordwant = rand()%58110; //Maximum is 58109 which is the last word, minimum is 0.
for (int linenum = 0; getline (wordfile, wordonline) && linenum <(wordwant+1) ; linenum++) {
if (linenum == wordwant) {
word = wordonline;
}
}
wordfile.close();
return word;
}
// gets random words based on number of words supplied.
void getWords() {
cout << "WORD GET" << endl;
string thisword;
for (int i=0 ; i<num_words; i++) {
thisword = randomWord();
pass_words.push_back(thisword) ;
cout << pass_words[i] << " " ; //debugging
}
cout << endl; //debugging
return;
}
Here is a sample output
WORD GET
posingdsate
What am I doing wrong? Also apparently, when I put another loop for the pass_words vector, I can see the values, but never after or within the getWords() function.
Here's the other loop's output:
housemaids
--
necessitate
--
contacts
--
kampala
--
pion
--
scooped
--
posing
--