near the end of the function it should be:
else if(x >= width)
return (int (m_Caption.size() + 1));
the ">=" change occurs because it's possible the position is exactly after the last character, in which case the position is the last one, not 0 which is what it returns otherwise.
the "+ 1" change is there because the last position is actually one past the length. This is why when you clicked on textboxes to the right of text, it would go one before the last position instead of the last position.