mardi 25 février 2020

C++ Insert a random string into multiple places of another string

I wrote this code at the bottom and I want it to insert a random string r into my string str and multiple set intervals or at multiple random intervals (whichever is easier). I have a problem with the insert command in my for loop. Can anyone of the coding masters on StackOverflow help this 1st-year cs student out, please? Thank you in advance!!

int main() {
    string str;
    string rando;
    int len;
    int i;
    int randNum;
    int strLen;

    str = "This is just a random string of text!";

    //works great
    char letters[] = "abcdefghijklmnopqrstuvwxyz";
    srand(time(0));
    char x = letters[rand() % 26];
    char y = letters[rand() % 26];
    char z = letters[rand() % 26];
    string r;
    r = ".";
    r += x;
    r += y;
    r += z;  // output = "." + random 3 chars (for example ".nfg")

  for(auto i = str.begin(); i != str.end(); i += min<int>(str.end() - i, 14))
        i = (i != str.begin() ? str.insert(i,r) + 1 : i); // problem with str.insert
                                                          //error: "no matching member function for call 'insert'
    cout << str << endl;
}




Aucun commentaire:

Enregistrer un commentaire