mardi 17 novembre 2020

C++ String Outputs Invalid Characters After Concatenation And Crashes [duplicate]

I'm trying to print out random parts of a string and combine them together, but every 5 or 6 runs of my program it outputs unprintable characters:

like these

const std::string test = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWyxXYzZ123456789!@#$%&";

int st_length;

std::cout << "Enter Length: ";
std::cin >> st_length;

std::string output1;

std::uniform_int_distribution<int> t(1, test.length());
std::random_device testrd;

for (int i = 0; i < st_length; i++)
{
    output1[i] = test[t(testrd)];
}

for (int y = 0; y < st_length; y++)
{
    std::cout << output1[y];
}

Also, for some reason, the program always crashes when the length of the output string exceeds 20 and it also seems that the program prints out unprintable characters almost every time when the input is 20 and I have no clue why..




Aucun commentaire:

Enregistrer un commentaire