1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
// Random string generator
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
string Gen_RndStr(int, bool, bool, bool); // Generate a random string
int main() {
bool num, let, sym;
string numb, lett, symb;
cout << "\nUse numbers? [Y/N] ";
cin >> numb;
cout << "U\nse letters? [Y/N] ";
cin >> lett;
cout << "USe Symbols? [Y/N] " ;
cin >> symb;
if (numb=="Y" || numb=="y") num=true ;
else num=false;
if (lett=="Y" || lett=="y") let=true ;
else let=false;
if (symb=="Y" || symb=="y") sym=true ;
else sym=false;
int amt;
do {
cout << "How many characters to generate? ";
cin >> amt;
} while (amt<1 && amt>100);
cin.ignore();
int i;
cout << "Generate how many words? ";
cin >> i;
for (int j=0; j<i; j++) {
ofstream genfile;
genfile.open("genfile.txt");
if (genfile.is_open()) {
genfile << Gen_RndStr(amt, num, let, sym) << endl; // Write it to a file
}
else {
cout << "Error writing to file. Printing strings instead.";
cout << Gen_RndStr(amt, num, let, sym) << endl;
}
}
cin.get();
return 0;
}
// amount of characters, use letters? use numbers? use symbols?
string Gen_RndStr(int length, bool letters, bool numbers, bool symbols) { // Generate a random string:
string allChars; // A string containing all the possible characters.
string genstrin; // The final string, which will be returned to main.
srand(time(NULL)); // Seed 'rand()'
int i;
if (letters==true) {
for (i=65; i<90; i++) {
allChars+=static_cast <char> (i);
allChars+=static_cast <char> (i=32); // Use
}
}
if (symbols==true) {
for (i=33; i<47; i++) {
allChars+=static_cast <char>(i);
}
for (i=58; i< 64; i++) {
allChars+=static_cast <char>(i);
}
for (int i = 91; i <= 96; i++) {
allChars+=static_cast <char>(i);
}
for (int i = 123; i <= 126; i++) {
allChars+=static_cast <char>(i);
}
}
int possibilities=allChars.length();
for (i=0; i<30; i++) {
genstrin+=allChars[rand()%possibilities];
}
return genstrin;
}
|