What goes wrong in your program is your function Insert:
- You create a local variable of type std::string and in it compose a text string from the global char array and an insertion string.
- You determine a pointer to the internal character array used by the std::string object
- The function ends, which destroys all local variables, including the std::string object
- The function returns a pointer that now points to invalid memory
- your main program tries to print something that is declared to be a char array, but points to invalid memory. This causes undefined behaviour
You are fortunate your program didn't crash. Instead you get some random output.
All of this wouldn't be a problem if you would simply use std::string everywhere rather than needlessly converting back and forth to char array, which is a mistake anyway.
Other than that:
1. use #include <string>, not #include <string.h>
2. do not use #define. Instead use const definitions or using statements. In this case, the following would do the intended job just fine, and be much safer:
#using cstr = std::string; // declares an alias
#using std::cout; // lets you use cout without having to specify std::
3. do not use global variables. Instead pass the constants or variables to the functions that use them (see below)
4. do not use char arrays or char pointers. E. g. your function Insert should be declared and then called like this:
void Insert(std::string& targetString, size_t position, const std::string& insertion)
Insert(text, 1, "eee");
cout << text;
Note how you can pass a literal string to the Insert function even though it expects a std::string. This conversion is done implicitely and you don't need a function for that!
On a sidenote, in the above example, you can replace the call to Insert simply by this one line:
This would be much easier to read. There's no need to make it any more complex than that.