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
|
int main()
{
srand( unsigned( time( NULL ) ) );
const char Alpha[] = { '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', '\0' };
const int max_size = 15;
char Code[ max_size ] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', '\0' };
std::cout << "Alpha:\n" << Alpha << "\n\n";
std::cout << "Code:\n" << Code << "\n\n";
int choice = 0;
int alpha = 0;
int number = 0;
for( int i = 0; i < max_size; i++ )
{
choice = rand() % 2;
if( choice == 0 )
{
alpha = rand() % 26;
Code[ i ] = Alpha[ alpha ];
}
if( choice == 1 )
{
number = rand() % 10;
Code[ i ] = Alpha[ number ];
}
}
Code[ max_size - 1 ] = '\0'; //make the last element = \0 'NULL' as to end the 'cout' of the array
//max_size - 1, because when creating an array, elements are [ n - 1 ].
// array[ 10 ] = 0 through 9
std::cout << "Random code:\n" << Code << '\n';
return 0;
}
|