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.