0

I am making a random username generator and I am having issues with printing numbers after the randomly selected vector item.

Code:

#include <iostream>
#include <cassert>
#include <vector>
#include <fstream>
using namespace std;

int main () {
    // Initialize
    srand (time(NULL));
    int length = 0, quantity = 0, random = 0;
    
    // Prompt & Introduce user
    cout << "Random Username Generator!" << endl;
    cout << "Enter word length, 0 for random: ";
    cin >> length;
    cout << "How many words?: ";
    cin >> quantity;

    // Read in dictionary textfile
    ifstream fin;
    fin.open( "CSW_2019.txt" );
    assert( fin.is_open() );

    // Initialize vector
    vector<string> validWords;  
    string word = ""; 

    // Loop to build validWords vector
    while ( !fin.eof() ) { 
        getline(fin, word);
        if ( word.length()-1 == length || length == 0 ) { 
            validWords.push_back(word);
        }   
    }   

    // Loop to randomly display words and 1-2 digits following
    for ( int i = 0; i < quantity; i++ ) { 
        // Random in-range value
        random = rand() % validWords.size();

        // Print random word
        cout << validWords[random] << flush;

        // Print number between 0 and 99
        cout << random % 100 << endl;
    }   

    // Close file because I wasn't raised in a barn
    fin.close();
}

Output:

Random Username Generator!
Enter word length, 0 for random: 4
How many words?: 4
92IN
24RE
92PS
31OR

My question is NOT about how to print a c++ vector, because it is simple. My question is seeking understanding for the strange printing output.

Here is an example of output that does not implement the line cout << random % 100 << endl; and changes the above << flush; to << endl; instead.

Output (not implementing numbers):

Random Username Generator!
Enter word length, 0 for random: 4
How many words?: 4
DOLT
RAYS
MOLE
BELT

Any and all help is appreciated, thank you all in advance.

Centixel
  • 47
  • 7
  • 1
    What system are you running on? Does the input contain UNIX or DOS style line endings? – ephemient Oct 10 '20 at 02:49
  • @ephemient I am running on macOS 10.13.6. As far as the input containing UNIX or DOS style line endings, I am unsure of how to verify either case. – Centixel Oct 10 '20 at 03:00
  • 1
    Does the command `file CSW_2019.txt` return `ASCII text`, `ASCII text, with CR line terminators`, or `ASCII text, with CRLF line terminators`? By your symptoms it may be the last one, which means you have extraneous `\r` in your word strings. – ephemient Oct 10 '20 at 03:04
  • 1
    Recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Oct 10 '20 at 03:06
  • @ephemient The command, ```file CSW_2019.txt``` returns ```CSW_2019.txt: ASCII text, with CRLF line terminators``` Which confirms the claim of carriage returns in the ```word``` strings. Damn, this was driving me up the wall. I really appreciate your help. – Centixel Oct 10 '20 at 03:09

0 Answers0