char RandomUpperChar()
{
int charOne;
int charTwo;
int charThree;
int charFour;
const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
srand (time(nullptr)); // Initialize random seed
charOne = rand() % 26; // Generate a random number from 1-26, representing the alphabet
charTwo = rand() % 26;
charThree = rand() % 26;
charFour = rand() % 26;
int P[4] = {charOne, charTwo, charThree, charFour};
for (int i = 0; i < 4; i++) {
std::cout << characters[P[i]] << std::endl;
}
}
I get the expected output. Now what I want to do is push each letter generated to one array. In JavaScript, it would be something like push(). Is there something similar I should use? What's the best way to approach this?
Also, I feel as if using charOne, charTwo etc to generate these characters can be written more eloquently. How should I go about that?
As for the generation of the characters, there's no saying, since I don't exactly know what the program is trying to do. What you did is fine though. Everyone has their own way of design.
Good on rewriting the code so that you don't have to initialize it one by one, BUT...
1 2 3 4 5 6 7 8
int i; // i is actually not initialized here
int arr[i]; // who knows what array size you are actually creating?
int P[4];
const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 1; i <=4; i++) {
arr[i] = rand() % 26; // What you're doing here is actually very dangerous
// You're accessing memory that should not be accessible
// which leads to memory leaks.