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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
constexpr int MAXSIZE{ 5000 }; // <--- Can be used in both functions.
void RandomNameGen(std::string names[]); // <--- Changed function name.
int main()
{
std::string names[MAXSIZE];
//srand(time(NULL));
srand(static_cast<unsigned int>(time(nullptr)));
RandomNameGen(names); // <--- Changed function name.
//cout << "name is: " << RandomFirstGen() << endl; // <--- As mentioned this will only produce 1 name.
for (size_t idx = 0; idx < 20; idx++) // <--- Used for testing. Comment or remove when finished.
{
std::cout << std::setw(4) << idx + 1 << ". " << names[idx] << '\n';
}
return 0; // <--- Not required, but makes a good break point.
}
void RandomNameGen(std::string names[]) // <--- Changed function name.
{
//const int MAX = 26;
constexpr int MAX{ 26 };
const char alphabet[MAX] // <--- Could use a "std::string" just as easily.
{
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z'
};
for (int i = 0, idx = 0; i < MAXSIZE; i++, idx++) // <--- Changed. The <= would generate 5001 names putting you past the end of the array.
{
string name;
int namelength = rand() % (16 - 8 + 1) + 4 ;
for (int i = 0; i < namelength; i++)
{
rand(); // <--- Added. Sometimes it helps.
name = name + alphabet[rand() % MAX];
}
names[idx] = name;
}
}
|