It’s not random, it’s whatever happens to be in that piece of memory. Bugs like this can cause programs to leak information they’re not meant to share, for example a first string might be a username and the next one might be a password. Keep reading and you get both.
#include <cstring>
#include <iostream>
struct user {
char user[100];
char password[100];
};
int main() {
user bob;
// zero out the fields to reduce spam in output
memset(bob.password, 0, sizeof(bob.password));
memset(bob.user, 0, sizeof(bob.user));
// write to the string fields
strcpy(bob.user, "bobthegreat");
strcpy(bob.password, "123carrotlover");
for (int i = 0; i < 200; i++) {
// note only reading from user, not password
std::cout << bob.user[i];
}
std::cout << std::endl;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string funnyString = " y a;er^&%&^%^&$%&$%7667567m.";
string otherString = " 43098rf39rk34fertkibuertbuiertb045b645456$#^#$%^6$#%^$#%^$%&^$^&@#$#!$#$@$`~~```~``";
for (int x = 0; x<10; x++)
cout<< funnyString[x];
for(int x=0; x<10; x++)
cout<<otherString[x];
}
I changed a few things and got the strings to print out correctly. They only print to the tenth character, but you could change that to whatever to print the whole string out.