1

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
--
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
offshore
  • 11
  • 3
  • `rand()` is pretty trash, and `RAND_MAX` might not be as high as you expect. In C++ consider using [`std::uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution). Also having a hard-coded "number of lines" value in there is a huge assumption. Why not read the entire file, then pick a line randomly? This would be more efficient than your current approach of re-reading the whole file each time you pick a word. – tadman Feb 20 '21 at 06:00
  • ⟼Remember, it's always important, *especially* when learning and asking questions on Stack Overflow, to keep your code as organized as possible. [Consistent indentation](https://en.wikipedia.org/wiki/Indentation_style) helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on. – tadman Feb 20 '21 at 06:01

1 Answers1

0

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;
tadman
  • 208,517
  • 23
  • 234
  • 262
  • Thank you, thats how the second loop works, it prints all entries on separate lines. – offshore Feb 20 '21 at 06:12
  • Watch for contaminated data. When in doubt, step into your debugger and look at the exact bytes in the string. I bet you have some CR (decimal 13, hex 0x0D) characters. – tadman Feb 20 '21 at 06:13
  • 1
    I redid the file, (copied the entire text but removed all other data), IT WORKED! (Thank you so much, my god I've spent hours just trying to fix that) – offshore Feb 20 '21 at 06:44
  • Great to hear! Upvote any answers that help, and accept those that solve the problem. – tadman Feb 20 '21 at 06:51