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
--
There is a very high probability that you're reading in CR characters from a CRLF delimited file, assuming it is just LF delimited, and printing them, where the carriage return will slam the cursor to the left and write over existing text.
You will need to either read this as CRLF or strip out any CR or LF characters.
You can also force a newline:
std::cout << pass_words[i] << std::endl;