I'm working on my own string class called PString, I have this function that finds a specific character, like 6, and now I have this function called substr short fo substract, where I want to substract from 0 to [insertnumber]. the way I'm trying to call this is by doing this:
PString string;
int to = 0;
string = "A string = a string"; // lol
string.find('=', to);
string.substr(to);
My find function is fine and works 100%, but my substr function doens't work. Here is my substr function:
void PString::substr(int to) {
char* temp = new char[255];
temp = this->c_str();
for (int i = 0; i < to; i++) {
temp[i] = ' ';
}
}
this->c_str() just returns the current string the object is holding, this is to prevent failure, but I cannot get past the first for loop, temp[i] = ' ' just throws a exception and I'm completely clueless as to why.
Oh and yes, I know my function isn't deleting temp and more stuff, but this just hasn't been implemented as I'm trying to get the for loop to work first.
Edit fixed by makign a few more functions (makign a new PString from a already created PString) Also used memcpy instead of just doing a simple equals operator